zoukankan      html  css  js  c++  java
  • 全局组件、prop示例

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="../js/vue.js"></script>
    </head>
    <body>
        <div id="app">
            <!--   循环遍历posts数组      在调用组件时,为自定义的属性赋值 -->
            <boke v-for="post in posts" v-bind:id="post.id" v-bind:title="post.title" v-bind:content="post.content"></boke>
        </div>
        <script>
            // 全局注册组件
            Vue.component(
            // 组件名
                "boke", {
                // 在props列表里自定义属性,自定义属性可以是任意数量,属性太多不利于维护
                props: ['title', 'id', 'content'],
                // html模板 组件实例中访问这个值就像访问data中的值一样
                template: "<div>{{id}}-----{{title}}-->{{content}}</div>",
            },
            );
            // 全局注册的组件可以用在其被注册之后的任何(通过new Vue())新创建的实例中
            var vm = new Vue({
                el: "#app",
                data: {
                    posts: [
                        { id: 1, title: "海贼王", content: '为自由和梦想冲冲冲' },
                        { id: 2, title: '奥特曼', content: '为地球和平而战' }
                    ]
                }
            });
        </script>
    </body>
    </html>
     
    改进版
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="../js/vue.js"></script>
    </head>

    <body>
        <div id="app">
            <!--   循环遍历posts数组    -->
            <boke v-for="post in posts"  v-bind:post="post"></boke>
        </div>
        <script>
            // 全局注册组件
            Vue.component(
                // 组件名
                "boke", {
                // 在props列表里自定义属性
                props: ['post'],
                // html模板 组件实例中访问这个值就像访问data中的值一样
                template: "<div> {{post.id}}-----{{post.title}}-->{{post.content}}</div>",
            },

            );
            // 全局注册的组件可以用在其被注册之后的任何(通过new Vue())新创建的实例中
            var vm = new Vue({
                el: "#app",
                data: {
                    posts: [
                        { id: 1, title: "海贼王", content: '为自由和梦想冲冲冲' },
                        { id: 2, title: '奥特曼', content: '为地球和平而战' }
                    ]
                }
            });
        </script>
    </body>

    </html>
  • 相关阅读:
    光棒效果的几种方法
    jQuery中的事件与动画
    jQuery中.bind() .live() .delegate() .on()的区别
    JavaScript基础
    jQuery选择器课堂随笔
    Leetcode_34【在排序数组中查找元素的第一个和最后一个位置】
    Leetcode_33【搜索旋转排序数组】
    Leetcode_32【最长有效括号】
    Leetcode_31【下一个排列】
    Leetcode_30【串联所有单词的子串】
  • 原文地址:https://www.cnblogs.com/kukai/p/12391886.html
Copyright © 2011-2022 走看看