1

e.g:
I need to add data-value after new vue and work for data-binding.

why i need to do that :

I know it's work if i declare first on new Vue object.
But there's a old project with a lot of layout-subject-page,if i need to add a vue global varible then i need to edit every page's vue data-value.

what i expected :

var app = new Vue({
  el: '#app',
  data: {}
})
app.$data.message = "test"
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <h1>{{ message }}</h1>
</div>

2
  • 1
    Are you wanting to dynamically add a new data prop and update the template to use that prop. While the former is possible, I don't believe the latter is. Commented Apr 13, 2020 at 3:32
  • @tony19 yes,i want it. dynamically add a new data prop and update the template to use that prop and work for adding after new Vue object Commented Apr 13, 2020 at 3:35

1 Answer 1

1

It can't be done.

Vue runs internal setup on the initially declared data properties which won't happen if you try to declare them later.

From the docs:

Vue does not allow dynamically adding new root-level reactive properties to an already created instance.

You can add new object/array items using Vue.set if that helps you.

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

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.