A
A
Artem Prokhorov2021-05-04 23:10:11
Vue.js
Artem Prokhorov, 2021-05-04 23:10:11

How to pass variable from vanilla js to Vue object?

For example, I declared a variable in js: Below (the Vue object is declared in the code itself): PS Simply declaring in data: nums[], is not an option for me, since I need an already formed array obtained earlier.

let nums = [1, 2, 3];


let menu_change = new Vue({some code})


Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-05-04
@kotcich

let nums  = [1, 2, 3];

// 100 000 000 строк кода

let menu_change = new Vue({
  // 100 000 000 строк кода

  methods: {
    showArr(){
      console.log( nums )
    }
  }

  // 100 000 000 строк кода
})

What is the problem?

K
Klein Maximus, 2021-05-04
@kleinmaximus

https://ru.vuejs.org/v2/api/#provide-inject

const nums  = [1, 2, 3];

// Добавляем провайдера
const app = new Vue({
  provide: {
    nums,
  },
});

// Внедряем в компонентах
// дочерний компонент внедряет 'nums'
const Children = {
  inject: ['nums'],
  created () {
    console.log(this.nums) // => [1, 2, 3]
  }
  // ...
}


Note: The provide and inject bindings are NOT REACTIVE. This is done on purpose. However, if you pass down a tracked object, the properties on that object remain reactive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question