Vue way between the components of the traditional values

Prop

prop parent component is used to pass data to a custom attribute.

Data required by props parent component of the data to the sub-components, sub-assemblies need to explicitly declare with props option "prop":

<div id="app">
    <child message="hello!"></child>
</div>
 
<script>
// 注册
Vue.component('child', {
  // 声明 props
  props: ['message'],
  // 同样也可以在 vm 实例中像 "this.message" 这样使用
  template: '<span>{{ message }}</span>'
})
// 创建根实例
new Vue({
  el: '#app'
})
</script>

 

Dynamic Prop

HTML attributes with similar binding to a v-bind expression, the value of props may be dynamically bound by v-bind to the data in the parent assembly. Whenever data changes parent element, this change will be conducted to the sub-assembly:

<div id="app">
    <div>
      <input v-model="parentMsg">
      <br>
      <child v-bind:message="parentMsg"></child>
    </div>
</div>
 
<script>
// 注册
Vue.component('child', {
  // 声明 props
  props: ['message'],
  // 同样也可以在 vm 实例中像 "this.message" 这样使用
  template: '<span>{{ message }}</span>'
})
// 创建根实例
new Vue({
  el: '#app',
  data: {
    parentMsg: '父组件内容'
  }
})
</script>

 

 

Custom Event

Parent component is the use of props to transfer data to sub-assemblies, subassemblies but if want to transfer data back, you need to use custom events!

We can use the v-on to bind custom events and each instance Vue implements the event interface (Events interface), namely:

  • Use  $on(eventName) listens for events
  • Use  $emit(eventName) trigger events

In addition, the parent component can be directly used v-on to listen to events triggered by the subassembly where the use of sub-assemblies.

The following example neutron components and it has been completely decoupled the outside. All it does is trigger an internal event a parent component of concern:

<div id="app">
    <div id="counter-event-example">
      <p>{{ total }}</p>
      <button-counter v-on:increment="incrementTotal"></button-counter>
      <button-counter v-on:increment="incrementTotal"></button-counter>
    </div>
</div>
 
<script>
Vue.component('button-counter', {
  template: '<button v-on:click="incrementHandler">{{ counter }}</button>',
  data: function () {
    return {
      counter: 0
    }
  },
  methods: {
    incrementHandler: function () {
      this.counter += 1
      this.$emit('increment')
    }
  },
})
new Vue({
  el: '#counter-event-example',
  data: {
    total: 0
  },
  methods: {
    incrementTotal: function () {
      this.total += 1
    }
  }
})
</script>

 

Jump pages pass value

1. Go through the router-link

<router-link 
    :to="{
    path: 'name',
    params: {
        key:value
    },
    query: {
        key: value
    }
}">
    跳转
</router-link>

 

 

2. $ router mode Jump

. This $ router.push ({name: 'Named Route', params: {Parameter name: Parameter value Parameter name: Parameter Value}})

Of acceptance

this. $ route.params. parameter name

this. $ route.query. parameter name

 

 

Published 81 original articles · won praise 62 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_38021852/article/details/88646997