Home > front end >  Am I able to use properties on the index.html file on the vuejs mount element?
Am I able to use properties on the index.html file on the vuejs mount element?

Time:11-10

<!DOCTYPE html>
<html lang="">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <link rel="icon" href="<%= BASE_URL %>favicon.ico" />
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <noscript>
      <strong
        >We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work
        properly without JavaScript enabled. Please enable it to
        continue.</strong
      >
    </noscript>
    <div id="app" thisIsAProp="This is what I need"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

So in my main.js file the app is mounted on #app. In my App.js file I need the properties on the <div id="app"> element. I tried using web-components which did work for a basic app but another requirement is I need to be able to overwrite the CSS variables within the parent web page, and when creating web components, vue creates them with a shadow-dom. Is there a better way to get the values from the vue mount element?

CodePudding user response:

Data can be passed from the the page to entry point either with global variables:

<script>
window.myApp = {
  foo: 'bar'
}
</script>
<div id="app"></div>

And accessed inside the app like window.myApp.foo.

Arbitrary string data (including JSON) can be passed through HTML attributes, data attributes commonly serve this purpose:

<div id="app" data-foo="bar"></div>

And accessed inside the app like document.querySelector('#app').dataset.foo.

CodePudding user response:

Solved my problem

I was able to get the properties from the index.html mount into my App.vue file using

  beforeMount() {
    this.neededData = this.$parent.$el.getAttribute("thisIsAProp");
  },
  • Related