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

How to pass parameters from parent to child component in vue.js

php中世界最好的语言
Release: 2017-12-31 10:33:55
Original
1590 people have browsed it

What I bring to you this time is how to pass parameters from parent to child components in vue.js. This article will give you a good analysis.

1. Create a new componentA.vue component with the following code:

const STORAGE_KEY = 'todos-vue.js'
export default{
  fetch(){
    return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '[]')
  },
  save(items){
    window.localStorage.setItem(STORAGE_KEY,JSON.stringify(items));
  }
}
Copy after login

App.vue code is as follows:


<template>
 <div id="app">
  <h1 v-text="title"></h1>
  <input v-model="newItem" v-on:keyup.enter="addNew"/>
  <ul>
   <li v-for="item in items" v-bind:class="{finished:item.isFinished}" v-on:click=&#39;toogleFinish(item)&#39;>
    {{item.label}}
   </li>
  </ul>
  <!-- 使用组件,注意驼峰命名法转化成短线 -->
  <!-- 向自组件传数据 -->
  <Component-a msgfromfather=&#39;you die!&#39;></Component-a>
 </div>
</template>
<script>
import Store from &#39;./store&#39;
import ComponentA from &#39;./components/componentA&#39;  //该组件会被加载到该页面
export default {
 name: &#39;app&#39;,
 data () {
  return {
   title: &#39;this is a todo list&#39;,
   items:Store.fetch(),
   newItem:&#39;&#39;
  }
 },
 components:{ //注册组件
  ComponentA
 },
 watch:{
   items:{
    handler(items){   //经过变化的数组会作为第一个参数传入
     Store.save(items)
    },
    deep:true    //深度复制
   }
 },
 methods:{
  toogleFinish(item){
   item.isFinished = !item.isFinished
  },
  addNew(){
   this.items.push({
    label:this.newItem,
    isFinished:false,
   })
   this.newItem = &#39;&#39;
  }
 }
}
</script>
<style>
#app {
 font-family: &#39;Avenir&#39;, Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
}
.finished{
 text-decoration: underline;
}
</style>
Copy after login

componentA.vue code As follows:


<template>
 <div class="hello">
  <h1>{{msg}}</h1>
  <h2>{{msgfromfather}}</h2>
  <button v-on:click="onClickMe">Click!</button>
 </div>
</template>
<<script>
export default {
 data(){
   return{
     msg:&#39;Hello form component a&#39;
   }
 },
 props:[&#39;msgfromfather&#39;],//自组件接收数据
 methods:{
   onClickMe(){
     console.log(this.msgfromfather);
   }
 }
}
</script>
<style scoped>
</style>
Copy after login

I believe you have mastered the method after reading the above introduction. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:


How to use Vue’s custom instructions to complete a drop-down menu

How can JS click to jump to the logged-in personal mailbox

nvm's method of managing different versions of node

The above is the detailed content of How to pass parameters from parent to child component in vue.js. 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