Home > Web Front-end > JS Tutorial > Detailed explanation of the steps to pass values ​​from the Vue parent component to the parent component

Detailed explanation of the steps to pass values ​​from the Vue parent component to the parent component

php中世界最好的语言
Release: 2018-04-27 15:49:06
Original
1779 people have browsed it

This time I will bring you a detailed explanation of the steps for passing values ​​from the vue parent component to the parent component. What are the precautions for passing values ​​from the vue parent component to the parent component? The following is a practical case, let's take a look. one time.

As shown in the figure below:

When there is no operation, the value of the parent component is 0

Detailed explanation of the steps to pass values ​​from the Vue parent component to the parent component

When the plus sign is clicked, the parent component The value of the component is 1

Detailed explanation of the steps to pass values ​​from the Vue parent component to the parent component

When the minus sign is clicked, the value of the parent component is reduced by one and becomes 0

Detailed explanation of the steps to pass values ​​from the Vue parent component to the parent component

I'll post the specific code directly, it's just released.

nbsp;html>


  <meta>
  <meta>
  <meta>
  <title>子组件将数据传递给父组件</title>
  <script></script>

<script>
//定义一个组件
Vue.component(&#39;counter&#39;, {
 template: &#39;\
    <p style="background:#eee;width: 238px;">\
        <p>这里是子组件里面的内容!\
        <p style="margin-top:20px">\
        <p>\
          <span style="margin-right:20px;display:inline-block;">加法运算<button @click="incrementCounter">+\
        \
        <p>\
          <span style="margin-right:20px;margin-top:20px;display:inline-block;">减法运算<button @click="deleteCounter">-\
        \
    \
  &#39;,
 data: function () {
  return {
   counter: 0
  }
 },
 methods: {
  incrementCounter: function () {
   this.counter += 1;
   this.$emit(&#39;increment&#39;,1);
  },
  deleteCounter: function () {
   this.counter -= 1;
   this.$emit(&#39;increment&#39;,2);
  }
 }
})
//执行一个组件
window.onload = function(){
  var app = new Vue({
    el: &#39;#app&#39;,
    data: {
      total: 0
    },
    methods:{
      incrementTotal: function (val) {
        if(val==1){
          this.total += 1;
        }else{
          if(this.total<=0){
            this.total = 0;
          }else{
            this.total -= 1;
          }
        }
      }
    }
  })
}
</script>

  <p>
    </p><p>这里是父组件里面的内容!</p>    
    <p>子组件传递的值:<b>{{ total }}</b></p>
    <counter></counter>
  

Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Vue implements two-way data binding function (with code)

ES6 imitates Vue to implement two-way binding Defined function

The above is the detailed content of Detailed explanation of the steps to pass values ​​from the Vue parent component to the parent component. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template