5

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.

1
  • 1
    Why don't you just pass it to the Vue instance? data : { config : config }, Commented Dec 13, 2016 at 17:30

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.