VueJS Data Transfer Through Visualization

I am currently working with VueJS 2, I would like to pass some parameters from HTML to the VueJS component. Let me show you.

My Html Div is as follows:

<div id="app" :id="1"></div>

And my javascript:

new Vue({
  store, // inject store to all children
  el: '#app',
  render: h => h(App)
});

My application component:

<template>
  <div>
    {{ id }}
  </div>
</template>
<script>
export default {
  props: {
   id: Number
  }
}
</script>

I would like to get the identifier passed in Html in my App component. How can I do it?

+6
source share
1 answer

Here is one way.

<div id="app" data-initial-value="125"></div>

new Vue({
  el: '#app',
  render: h => h(App, {
    props:{
      id: document.querySelector("#app").dataset.initialValue
    }
  })
})

But you do not need to use the rendering function.

new Vue({
  el: '#app',
  template:"<app :id='id'></app>",
  data:{
    id: document.querySelector("#app").dataset.initialValue
  },
  components:{
    App
  }
})

, querySelector, , initialValue ( id) , - , . .

+3

Source: https://habr.com/ru/post/1015806/


All Articles