1.全局组件componet的定义

1.1 代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全局组件componet的定义</title>
    <style>
        * { 
            padding: 0;
            margin: 0;
        }

        body {
            background: #f4f4f4;
        }

        .box {
            width: 200px;
            border: 1px solid #aaa;
            padding: 5px;
            background: #fff;
            margin: 10px;
        }

        .box img {
            width: 100%;
        }

        .box h2 {
            font-size: 18px;
            font-weight: 400;
        }

        .box .price {
            font-size: 28px;
            color: #c00;
            font-weight: 600;
        }
    </style>
</head>
<body>
    <div id="app">
        <itembox></itembox>
        <itembox></itembox>
        <itembox></itembox>
        <itembox></itembox>
        <!-- <div class="box">
            <img src="./imgs/img1.jpg" alt="">
            <h2>男士休闲裤</h2>
            <div class="price">¥98.9</div>
        </div> -->
    </div>

    <script src="./js/vue.js"></script>
    <script>

        //vue全局组件
        //1.先取一个组件名
        Vue.component("itembox",{
        //     template: '<div class="box"><img src="./imgs/img1.jpg" alt=""><h2>男士休闲裤</h2><div class="price">¥98.9</div></div>'
            template: `
                <div class="box">
                    <img src="./imgs/img1.jpg" alt="">
                    <h2>男士休闲裤</h2>
                    <div class="price">¥98.9</div>
                </div>
            `
    })

        new Vue({
            el: "#app"
        })
    </script>
</body>
</html>

1.2 显示效果

1.3 在线演示

点击下面链接,即可在线运行

http://www.68yun.top/demo/vue/2.3.html

最后修改:2021 年 02 月 22 日
如果觉得我的文章对你有用,请随意赞赏