Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js: access global value in template string

I have a very basic Vue.js app that looks like this:

index.html (just the <body> for succinctness)

<div id="root"></div>

main.js

var config = {
  title: 'My App',
}

new Vue({
  el: '#root',
  template: '<div>{{ config.title }}</div>',
})

This gives me:

Property or method "config" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. 
(found in root instance)

I'm guessing it's because Vue is looking for it in the data store rather than window. Is there some way to signal that config is a global here, or a better way to do this entirely? I suppose I could pass the config object as a prop to my root Vue instance, but it seems like only components accept props. Thanks for any insights on this.

like image 397
serverpunk Avatar asked Dec 13 '16 17:12

serverpunk


People also ask

How do you access global variables in Vue?

Accessing global variable We can access the global variable from any component instance by using this. $variablename .

How do I use a Vue template in JavaScript?

Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers.

What does template tag do in Vue?

In Vue, templating is the main way we create reusable components. We can use templates to take data, and turn it into real elements on a screen that users can see.


2 Answers

You can try to define your app as follows:

new Vue({
  el: '#root',
  template: '<div>{{ config.title }}</div>',
  data: function() {
    return {
      config: window.config
    }
  }
})

In the above definition, your this.config within the root component points to window.config - the global variable.

like image 83
Mani Avatar answered Oct 14 '22 13:10

Mani


If you are just trying to pass the App Name in as a hard-coded string you can do something like this:

var nameMyApp = new Vue({
  el: '#root',
  data: { input: 'My App' },
  computed: {
    whosapp: function () {
      return this.input
    }
  }

})

With your html like this:

<div id="root">
  <div v-html="whosapp"></div>
</div>

Which will pass the value of input to the whosapp div.

But, if you're looking for something more dynamic, this would be the way to go:

With the same html as above...

var nameMyApp = new Vue({
  el: '#root',
  data: { input: 'My App' },

The initial value is set to My App, which is what will display if not changed through a function call on the method.

    methods: {
    update: function(name) {
        this.input = name;
    }
  },

Here is where we define a method that, when called, passes in the parameter to update this.input.

  computed: {
    whosapp: function () {
      return this.input
    }
  }

})

nameMyApp.update("New Name");

And here is where we call the function, giving it the new parameter; making sure that the parameter is a string.

I hope this is what you're looking for.

like image 30
Clay Allen Avatar answered Oct 14 '22 14:10

Clay Allen