zoukankan      html  css  js  c++  java
  • vue 组件

    组件可以复用,注意全局组件最好使用my-component-name 命名,在VScode上遇过坑

    一、Vue对象

    若Vue对象中有template,那么template的优先级高于,原本的视图

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <div id="app">
    
        </div>
        <script src="./js/vue.js"></script>
        <script>
            new Vue({
                el: "#app",
                data(){
                    return {
                        msg: "点我",
                    }
                },
                template: `
                    <div>
                        <button>{{msg}}</button>
                    </div>
                `
            })
        </script>
    </body>
    </html>

    二、局部组件

    注意:

    1、单词拼写

    2、子组件是 组件名 = {}

    3、声明、挂载、引用

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <div id="app">
            <!-- <button>{{msg}}</button> -->
        </div>
        <script src="./js/vue.js"></script>
        <script>
            // Vue(父)->App(子)
            //1. 声明子组件
            let App = {
                data(){
                    return {
                        text:"咬我",
                    }
                },
                template: `<p>{{text}}</p>`,
            }
            new Vue({
                el: "#app",
                data(){
                    return {
                        msg: "点我",
                    }
                },
                // 若 Vue对象有template,则template优先级高
                // 3.引用子组件
                template: `
                    <div>
                        <button>{{msg}}</button>
                        <App></App>
                    </div>
                `,
                // 2.挂载子组件
                components:{
                    App,
                }
            })
        </script>
    </body>
    </html>

    一、全局组件

    注意:全局组件不用挂载

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <div id="app">
            <h2>{{msg}}</h2>
            <my-button></my-button>
        </div>
        <script src="./js/vue.js"></script>
        <script>
            Vue.component("my-button", {
                data(){
                    return {
    
                    }
                },
                template: '<button>点击</button>'
            })
            var app = new Vue({
                el: "#app",
                data: {
                    msg: "组件",
                },
                methods: {},
                computed:{},
            })
        </script>
    </body>
    </html>

    二、模块系统

    <template>
        <div id="app">
            <h3>{{msg}}</h3>
            <Vhead></Vhead>
            <Vcontent></Vcontent>
            <Vfoot></Vfoot>
        </div>
    </template>
    
    <script>
    import Vhead from "./components/Vhead"
    import Vcontent from "./components/Vcontent"
    import Vfoot from "./components/Vfoot"
    export default {
        name: "App",
        data(){
            return{
                msg: "第一个Vue项目",
            }
        },
        methods: {},
        computed: {},
        components:{
            Vhead,
            Vcontent,
            Vfoot,
        },
    }
    </script>
    
    <style scoped>
    
    </style>
  • 相关阅读:
    .net验证是否合法邮箱和ip地址的方式
    .net通用类型转换方法
    asp.net中的<%%>的使用
    autofac初识
    .net面试题
    asp.net使用一般处理程序实现文件下载
    asp.net 一般处理程序接收上传文件的问题
    Python学习日记(十八) 序列化模块
    Python学习日记(十七) os模块和sys模块
    Python学习日记(十六) time模块和random模块
  • 原文地址:https://www.cnblogs.com/wt7018/p/11494283.html
Copyright © 2011-2022 走看看