zoukankan      html  css  js  c++  java
  • Vue 给子组件传递参数

    Vue 给子组件传递参数

    1. 首先看个例子吧

    原文

    html

    <div class="container" id="app">
        <div class="row">
            <div class="col-sm-12">
                <h3>My Components</h3>
                <todo-item :todos="todos01"></todo-item>
            </div>
        </div>
    </div>
    <script type="x-template" id="component-todo">
        <ul class="list-group" v-if="todos && todos.length > 0">
            <li class="list-group-item" v-for="todo in todos" :class="{special: !todo.isSpecial}">
                {{todo.title}}
                <button class="btn btn-xs  pull-right" :class="{'btn-success': todo.isSpecial,'btn-danger': !todo.isSpecial }" @click="changeActive(todo)">
                    {{todo.isSpecial ? 'DONE' : 'What?'}}
                </button>
            </li>
        </ul>
        <div v-else>
            <p>There isn't any todo</p>
        </div>
    </script>

    js

    var todoItem = Vue.extend({
        template: '#component-todo',
        props: ['todos'],
        methods: {
            changeActive(todo) {
                todo.isSpecial = !todo.isSpecial;
            }
        }
    })
    Vue.component('todo-item', todoItem);
    new Vue({
        el: '#app',
        data: {
            todos: [{
                id: 1,
                title: 'zgo to shoping',
                isSpecial: false
            }, {
                id: 2,
                title: 'jump for sport',
                isSpecial: true
            }, {
                id: 3,
                title: 'welcome vueJs',
                isSpecial: true
            }],
            todos01: [{
                id: 1,
                title: 'so so crazy',
                isSpecial: true
            }, {
                id: 2,
                title: 'i v ini',
                isSpecial: false
            }, {
                id: 3,
                title: 'nothing is there',
                isSpecial: true
            }]
        }
    })

    <todo-item :todos="todos01"></todo-item>

    todos是组件中通过props传递过来的参数,todos01是组件上一层定义的

    可以尝试将todos01换成todos看看效果

  • 相关阅读:
    第一篇:spring boot 初始
    数据结构 -- 线段树
    数据结构 -- 优先队列和堆排序
    javaIO -- 流的体系设计思路、基础分类
    JavaIO -- Reader 和 Writer
    javaIO -- InputStream和OutStream
    javaIO -- File源码
    数据结构 -- 二叉树(Binary Search Tree)
    数据结构 -- 链表(LinkedList)
    数据结构 -- 栈(Stack)
  • 原文地址:https://www.cnblogs.com/keRee/p/6252718.html
Copyright © 2011-2022 走看看