Home > Web Front-end > JS Tutorial > body text

How to use vue subcomponent to pass data to parent component

php中世界最好的语言
Release: 2018-06-02 14:45:44
Original
1208 people have browsed it

This time I will show you how to use vue sub-components to pass data to parent components, and how to use vue sub-components to pass data to parent components. What are the precautions?The following is a practical case, let's take a look .

As shown in the figure below:

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

How to use vue subcomponent to pass data to parent component

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

How to use vue subcomponent to pass data to parent component

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

How to use vue subcomponent to pass data to 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:

How to use vue source code to parse the event mechanism

How to operate JS to obtain the city and geographical location of the user

The above is the detailed content of How to use vue subcomponent to pass data to 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