Vue.js学习详细课程系列–共32节(5 / 6)

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 Vue.js学习详细课程系列–共32节(5 / 6),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

25. 组件:表行组件

知识点

  • 制作表行组件

表行组件

为自己的页面表格编写表行组件。

综合例

<div id="myApp">
    <h1>2017年最期待的游戏是:</h1>
    <table border="1">
        <tr>
            <td>编号</td>
            <td>游戏名称</td>
        </tr>
        <my-row1></my-row1>
        <my-row2></my-row2>
        <my-row3></my-row3>
    </table>
</div>
<script>
    Vue.component('my-row1', {
        template: '<tr><td>(1)</td><td>塞尔达传说:荒野之息(3/3)</td></tr>'
    });    
    Vue.component('my-row2', {
        template: '<tr><td>(2)</td><td>新马里奥赛车(4/28)</td></tr>'
    });    
    Vue.component('my-row3', {
        template: '<tr><td>(3)</td><td>喷射乌贼娘2代</td></tr>'
    });    
    var myApp = new Vue({
        el: '#myApp', 
        data: {
        },
        methods: {
        },
    });
</script>

26. 组件:数据

知识点

  • 组件的数据函数

组件的数据

为Vue.js组件添加数据,使组件可以动态显示各种数据,注意是数据函数,不是数据属性。

综合例

<div id="myApp">
    <div>今天的天气是<today-weather/></div>
</div>
<script>
    Vue.component('today-weather', {
        template: '<strong>{{todayWeather}}</strong>',
        data: function(){
            return {
                todayWeather: '雨加雪'
            };
        }
    });
    var myApp = new Vue({
        el: '#myApp', 
    });
</script>

27. 组件:传递数据

知识点

  • 为组件传递数据

组件数据传递

制作可接受参数的组件。

综合例

<div id="myApp">
    <h1>您的成绩评价</h1>
    <test-result :score="50"></test-result>
    <test-result :score="65"></test-result>
    <test-result :score="90"></test-result>
    <test-result :score="100"></test-result>
</div>
<script>
    Vue.component('test-result', {
        props: ['score'],
        template: '<div><strong>{{score}}分,{{testResult}}</strong></div>',
        computed: {
            testResult: function() {
                var strResult = "";
                if (this.score < 60)
                    strResult = "不及格";
                else if (this.score < 90)
                    strResult = "中等生";
                else if (this.score <= 100)
                    strResult = "优秀生";
                return strResult;
            }
        }
    });
    var myApp = new Vue({
        el: '#myApp', 
    });
</script>

28. 组件:传递变量

知识点

  • 为组件传递变量数据

组件的数据

制作可接受变量参数的组件。

综合例

<div id="myApp">
    <div>请输入您的名字:<input v-model="myname"></div>
    <hr/>
    <say-hello :pname="myname" />
</div>
<script>
    Vue.component('say-hello', {
        props: ['pname'],
        template: '<div>你好,<strong>{{pname}}</strong>!</div>',
    });
    var myApp = new Vue({
        el: '#myApp', 
        data: {
            myname: 'Koma'
        }
    });
</script>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135675.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!