Create a new Vue.js application (version 2.0.0rc7) with specified attributes

My Vue.js application (version 2.0.0rc7) is structured using single page components, with the main component being named App. In order to render my app within a div identified by app, I have created a script named main.js:

import Vue from 'vue'
import App from './App.vue'

var vm = new Vue({
  el: '#app',
  render: h => h(App)
})

The current setup is functioning effectively with webpack handling imports. However, I am interested in making my application easily integratable for other developers to use as a drop-in solution on their websites. So, I am exploring ways to pass properties to the main App component.

For instance, I envision allowing developers to load my pre-built app through HTML script tags and then initialize it like so:

App(dataObj1, dataObj2, ..., '#id-of-div-element-to-mount')

Answer №1

After much experimentation, I devised a clever fix for my issue: I crafted an init function that accepts the required data as input and then proceeds to display the application on an HTML element. The beauty lies in making this init function accessible in the global space:

var initialize = function (data, element) {
  const appViewModel = new Vue({
    el: element,
    template: '<app :data="data"/>',
    components: {
      App
    },
    data () {
      return {
        data: data
      }
    }
  })
}

// retrieve existing namespace object, or generate an empty object if not found
var APP = window.APP || {}
APP = {
  init: initialize
}
window.APP = APP

This setup enables others to effortlessly integrate my constructed application with their own data into their HTML pages using APP.init(data, '#some-el-id').

To ensure smooth operation, it is essential for Vue to be compiled in standalone mode, necessitating the addition of the following line to the webpack configuration:

// ...
resolve: {
    alias: {vue: 'vue/dist/vue.js'}
},
// ...

Detailed instructions can be found here: https://github.com/vuejs/vue/wiki/Vue-2.0-RC-Starter-Resources#standalone-vs-runtime-builds

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

Restricting href Usage Based on Session

I find myself in a challenging situation without a clear solution at hand. Within this code, I am utilizing a link and a button for which I need to save the page in a database. Therefore, creating a server control is not an option as it will not be render ...

Securely Connecting to an MQTT Broker with a JavaScript Client (React) while Safeguarding Credentials

Looking to securely connect my React application to a Mosquitto MQTT broker without exposing sensitive credentials to users who can access the JavaScript code. Considering options for authentication, such as using an API with JWT authentication to retriev ...

How do I incorporate scrolling into Material-UI Tabs?

I am currently incorporating Material-ui Tablist into my AppBar component. However, I am facing an issue with the responsiveness of the tabs. When there are too many tabs, some of them become hidden on smaller screens. For more information on the componen ...

Is there a way to verify if the JSON Object array includes the specified value in an array?

I am working with JSON data that contains categories and an array of main categories. categories = [ {catValue:1, catName: 'Arts, crafts, and collectibles'}, {catValue:2, catName: 'Baby'}, {catValue:3, catName: 'Beauty ...

NextJS: encountered an error with fallback enabled

I have my NextJS setup on Vercel and here is how I configured getStaticPaths: const paths = posts.map((post) => ({ params: { player: post.player, id: post.id }, })) return { paths, fallback: true } However, when I set the fallback to true, I ...

Connect a series of numerical input fields in Vue.js

One of the tasks I'm working on involves managing a table filled with rows of information. Users can choose multiple rows by checking checkboxes in each row, and I need to keep track of how many rows are selected by updating the totalSelected value. ...

Generating Dynamic Image Sources with Vue 3 using Rollup

Dealing with dynamic image src using Rollup in a Vite-app: <img :src="??" alt=""> Any suggestions on how to achieve this without Webpack's require? ...

Having trouble loading and viewing large PDF files using XMLHttpRequest

I'm currently using a straightforward XMLHttpRequest to communicate with a PHP server script. fxhr.onload = function (data) { //console.log(JSON.parse(data.target.response)); var string = JSON.parse(data.target.response).data.content; ...

Leveraging the For loop in the MongoDB Aggregation pipeline

Seeking a more efficient way to query data in MongoDB. In my collection, I store hourly information for each user_id. Each document in the collection corresponds to a unique user_id and date combination. However, a user_id can have multiple entries with d ...

Display a <div> every hour

I am attempting to display a <div> element once every hour. However, the code seems to be malfunctioning when I include the following: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div i ...

Validation for duplicate usernames or emails in the sign-up action of a REST API

As I develop a REST API, it is crucial for me to ensure the uniqueness of usernames and email addresses for every user. I have meticulously set up my database models to contain the necessary properties for this purpose. However, when it comes to route logi ...

Verify if two items possess identical property values

There are 2 items: 1. obj1 = { bookRetail: 14.99, hierarchyDescription: "GUYS DENIM PANTS [ 0151 ]", isSelected: true, isAvailableInPivot: "Y", style: "VICE NWH NAVY WHITE DC [ M450MBON ]"} 2. obj2 = { bookRetail: 14.99, hier ...

A guide on verifying a username and password via URL using JavaScript

As I work on developing a hybrid application using the Intel XDK tool and jQuery Mobile framework for the user interface, one of my current tasks is implementing a login function. This function involves simply inputting a username and password and clicking ...

Steps to hide a div with jQuery when a user clicks outside of a link or the div itself:1. Create a click event

Here's a simple question - I have a link that when clicked, displays a div. If the user clicks away from the link, the div is hidden, which is good. However, I don't want the div to be hidden if the user clicks on it. I only want the div to disap ...

Using external directive to validate Access Form

As a newcomer to angular, I have created a form with validation that only enables the save button when the validation pass <input type="submit" value="Save" ng-disabled="!frmRegister.$valid" /> I implemented a directive to handle global form submis ...

Clarification on the order and frequency of AJAX XHR request onReadyStateChange events

Currently, I am in the process of learning how to create a basic stock quote tool using Python-Flask and Javascript. I have a specific interest in mastering plain Javascript. While my code is functional, I am puzzled by the fact that I receive 3 error mes ...

How come my Calendar is not showing up on the browser?

I came across a helpful guide on setting up a Calendar in HTML using JavaScript You can find it here: Unfortunately, the code I tried to use based on that guide isn't functioning as expected. <div class="row"> <div class="col-lg-4 text ...

What is the best way to retrieve the index of a specific clicked value within a table?

How can I retrieve the index of a specific clicked value from a table cell where I am loading card details? This is my table: <table class="metricTable" border="2" bordercolor="white" style="background-color:#066B12;"> <tr> ...

Manage both React keyboard and mouse events within a single function

I'm working on enhancing the accessibility of my web app by making it keyboard accessible. However, I am facing an issue when trying to handle both click and keyboard events using the '|' symbol in my function: Property 'key' does ...

Unlocking the contents of an array from a separate file in Next.js

I am developing a Quiz App that consists of two main pages: takeQuiz.js and result.js. My goal is to transfer the data from the multiple-choice questions (mcqs) in takeQuiz.js to be accessible in result.js. Utilizing router.replace(), I ensure that once th ...