How to implement Vue.js template into a different HTML document

I have successfully designed a template using vue.js. Now, I am seeking to utilize this template in another HTML file by referencing its template Id.

For example, <div id="widgetId"></div>

The template Id is labeled as widgetId.

In this scenario, my goal is to generate embed code that showcases the template.

However, despite my efforts, I have not been able to achieve any output. Is there a method available to access the Vue template in another HTML file when implementing this embed code directly?

Below, you can find the code I am employing:

<script>
window.onload = function() {

var div = document.createElement('div');
div.className = 'widget-pos';
div.id = 'widget-posId';
document.body.appendChild(div);

var iframe = document.createElement('iframe');
iframe.id = 'widget-iframeId';
iframe.src = "javascript:false";
document.getElementById("widget-posId").appendChild(iframe);

document.getElementById('widget-iframeId').contentWindow.document.write("
<html><body><div id=widgetId></div></body></html>");

var iframeScript = document.createElement('script');
iframeScript.setAttribute("src", "/packs/main.js");

var Vuescript = document.createElement('script');
Vuescript.setAttribute('src',"//cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js")

$("#widget-iframeId").contents().find("head").append(iframeScript);
$("#widget-iframeId").contents().find("head").append(Vuescript);
}
</script>

Answer №1

I highly recommend taking a look at this insightful piece.

An effective approach might involve importing your external template in the following manner:

Vue.component('my-component', {
  template: require('./myTemplate.html')
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure UseEffect is only triggered once with specific dependencies

Whenever the component is loaded, I have a method called useEffect() that runs once. useEffect(() => { (async function fetchData() { const results = await stateIsoAPI(); setStates(results); // API results to trigger other Use effect ...

Utilize AngularJS's OrderBy feature to iterate through the list for navigation in ascending order by Index

I am looking to customize the order of an Object-array using ng-repeat based on their index keys in ascending order. $scope.paneContainer = {books:[{}] } ; $scope.paneContainer.books[0].science = { title:"science", content:"web/app/views/partials/scienc ...

Utilizing ScrollView Component in React Native

I have developed a simple app that displays a collection of images with titles, resembling a film gallery where all available films can be viewed. However, I encountered an issue when trying to implement the ScrollView element as it does not allow for scro ...

Handling Removal of Selected Option in React Material-UI Autocomplete Single Selection

I am currently using material UI autocomplete to create a single-select dropdown. However, I have encountered an issue wherein the onChange event does not get triggered when I click the close button on the right side of the input. This prevents my state fr ...

What is the best way to execute a method in a component for each iteration in an *ngFor loop in Angular 2

Is there a way to call a method for each iteration of *ngFor and pass the iterated variable as a parameter? For example: <li *ngFor="let element of componentModel | keys">{{element.key}}--{{element.value}}</li> Then, in the component, I have ...

"Enhance your Vuetify v-select by learning how to effortlessly add the same item multiple times

Vuetify offers a select field component that allows users to choose options from a list for collecting information. You can find documentation here. This component functions with checkboxes, so each time you check an item, it gets added to the end of the ...

"Encountering a strange behavior in Vue.js: the created() hook

I made a custom feature to refresh my data from the database by clicking a button: <template> <base-projects :projects="projects" /> </template> <script> import { mapGetters } from 'vuex'; import Projects from './ ...

Triggering a jQuery click event to showcase the hidden content

I am attempting to replicate the functionality seen on the website. The issue I am facing is related to the event_content class. I want to display only a limited amount of content initially, and then hide the excess content. Upon clicking the plus class, ...

javascript onload button is selected

I am looking to create a button on page load for my login page in Chrome. I want the login button to be preselected when the page loads, and when I press Enter, the onclick function of the button should be triggered. Any advice on how to achieve this? Th ...

"Encountering a Syntax Error When Using request.post in Dojo Framework on Button Click

Here are three questions to consider: 1) What strategies can be utilized to streamline this code and reduce nesting and quote-related complications? 2) Are there any suggestions for addressing the parsing error mentioned below? I've already tested sep ...

Generate a collection of LatLng coordinates to represent a worldwide grid

I'm in search of a quick and easy solution, like a simple library or similar tool, that would allow me to call a function like createGlobalGrid(1000) and automatically generate a list of points on a geospatial surface, ensuring that each point is no m ...

Implementing server-side caching with axios: a step-by-step guide

Presently, I am in the process of developing an application that integrates a Laravel API with a Vue.js frontend. To handle caching in the API, I have implemented the spatie/laravel-responsecache package. When I directly access a specific URL, such as htt ...

Issue with HTTP POST Headers in XmlHttpRequest

I am currently attempting to pass a string using the XmlHttp method. Let me provide you with the code for better understanding: HTML <div id="greetings"> You are voting out <b style="color: #00b0de;" id=roadiename></b>. Care to explain ...

Locate identical values in Vuejs

When working with an array of objects, I need to check if a newly inserted object is a duplicate or not. this.duplicate = false; for(let item of this.items){ if(item.id && this.item.name === item.name) { t ...

Ending the Overlay

I am using an overlay: <div id="overlayer" class="overlayer"> <div id="board" class="board"></div> </div> Here are the CSS properties I have applied to it: #overlayer { position:fixed; display:none; top:0; left:0; width:100%; hei ...

How can I retrieve the body from a GET request using Express?

Every time I attempt to execute my code, it returns an error message indicating invalid text. app.post("/charge", (req, res) => { console.log(req.body) }) ...

Is there a way to insert a value into the input field using JQuery or JavaScript?

After reading my previous post on posting values into an input box, a commenter suggested using JQuery or Javascript. For more code and information, please refer to the link provided above. <label>Date</label>:&nbsp&nbsp <input sty ...

Add the $scope ng-click event to a previously hidden element once it becomes visible

If you need a clearer explanation, feel free to ask. I have incorporated a global search function into the header of my website. I am looking to show a separate input box for mobile search that utilizes the same ng-click event, but the input field remains ...

What is the best way to retrieve app.state in a Remix project when running a Cypress test?

One way Cypress can expose an app's state to the test runner is by using the following approach in React: class MyComponent extends React.Component { constructor (props) { super(props) // only expose the app during E2E tests if (window.C ...

What could be causing the TypeError that is preventing my code from running smoothly?

I can't seem to figure out why I'm constantly encountering the error message Cannot destructure property 'id' of 'this.props.customer' as it is undefined.. Despite double-checking my code and making sure everything looks corre ...