8.在组件中使用其他组件

未匹配的标注
  • 本系列文章为laracasts.com 的系列视频教程——Learn Vue 2: Step By Step 的学习笔记。若喜欢该系列视频,可去该网站订阅后下载该系列视频,支持正版
  • 视频源码地址:github.com/laracasts/Vue-Forms
  • 项目初始版本为Vue 2.1.3,教程还在更新中。

本节说明

  • 对应第 8 小节:Components Within Components

本节内容

本节我们开始学习在组件中引入组件。上一节我们定义了一系列任务列表,本节我们定义一个task-list组件来显示任务列表:

main.js

Vue.component('task-list',{
    template:`
        <div>
            <task v-for="task in tasks">{{ task.description }}</task>
        </div>
    `,

    data() {
        return {
            tasks:[
                {description:'Go to work',completed:false},
                {description:'Go to bank',completed:false},
                {description:'Go to store',completed:false},
            ]
        }
    }
})

Vue.component('task',{
    template:'<li><slot></slot></li>'
});

new Vue({
    el:'#root'
})

然后使用该组件:

index.html

<!DOCTYPE html>

<html>
    <head>

    </head>

    <body>
        <div id="root">
            <task-list>Go to work</task-list>
        </div>

        <script src="https://unpkg.com/vue@2.1.3/dist/vue.js"></script>

        <script src="main.js"></script>
    </body>
</html>

查看效果:
file

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 查看所有版本


暂无话题~