Vue学习笔记之组件基础

1、组件的定义

一般将 Vue 组件定义在一个单独的 .vue 文件中,称做单文件组件;当然也可以将组件直接定义在js文件中,如下js代码,定义一个组件BlogPost,通过props定义对外暴露属性title,父组件传递title,子组件根据title渲染html内容。

export default {
    props: ['title'],
    template: `
     <div class="blog-post">
         <h4>{
  { title }}</h4>
     </div>`
}

2、组件的使用

首先需要引用组件,语法格式import BlogPost from './BlogPost.js',然后在vue应用对象中注册组件,在components区域注册即可,最后在DOM元素内部使用标签格式,如代码<blog-post title="My jouney with Vue"></blog-post>应用子组件,并传递title属性给子组件渲染。

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<div id="app">
   <blog-post title="My jouney with Vue"></blog-post>
</div>
<script type="module">
    import BlogPost from './BlogPost.js'
    const { createApp } = Vue;
    createApp({
        components: {
            BlogPost
        },
    }).mount('#app');
</script>

3、组件的重复应用

可以使用v-for语法循环创建子组件,如代码所示:

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<div id="app">
    <blog-post v-for="post in posts" :key="post.id" :title="post.title"></blog-post>
</div>
<script type="module">
    import BlogPost from './BlogPost.js'
    const { createApp } = Vue;
    createApp({
        data() {
            return {
                posts: [
                    { id: 1, title: 'My jounery with Vue' },
                    { id: 2, title: 'Blogging with Vue' },
                    { id: 3, title: 'Why Vue is so fun' }
                ]
            }
        },
        components: {
            BlogPost
        }
    }).mount('#app');
</script>

4、给组件传递事件

组件中模板声明一个按钮,并使用@click绑定点击事件,使用$emit抛出一个事件名为enlarge-text,并通过emits定义对外抛出的事件名称enlarge-text,定义如下代码所示:

export default {
    props: ['title'],
    emits: ['enlarge-text'],
    template: `
     <div class="blog-post">
         <h4>{
  { title }}</h4>
         <button @click="$emit('enlarge-text')">Enlarge text</button>
     </div>`
}

应用组件,应用中定义字体大小属性postFontSize,在组件blog-post应用的代码处定义事件@enlarge-text="postFontSize+=0.1",点击后触发字体+0.1:

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<div id="app">
    <div :style="{ fontSize: postFontSize + 'em' }">
        <blog-post v-for="post in posts" :key="post.id" :title="post.title" @enlarge-text="postFontSize+=0.1"></blog-post>
    </div>
</div>
<script type="module">
    import BlogPost from './BlogPost.js'
    const { createApp } = Vue;
    createApp({
        data() {
            return {
                posts: [
                    { id: 1, title: 'My jounery with Vue' },
                    { id: 2, title: 'Blogging with Vue' },
                    { id: 3, title: 'Why Vue is so fun' }
                ],
                postFontSize:1
            }
        },
        components: {
            BlogPost
        },
        mounted() {
            
        }
    }).mount('#app');
</script>

5、插槽的应用

在组件中定义一个插槽,模板中语法<slot/>,

export default {
    template: `
     <div class="alert-box">
         <strong>This is an Error for Demo Purpose</strong>
         <slot/>
     </div>`
}

在vue应用中给插槽传递html内容,alert-box元素中的内容“Something bad happened.”将传递给组件的插槽:

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<div id="app">
    <alert-box>
        Something bad happened.
    </alert-box>
    <table>
        <tr>
            <td>123</td>
            <td>456</td>
        </tr>
        <tr is="vue:alert-box"></tr>
    </table>
</div>
<script type="module">
    import AlertBox from './AlertBox.js'
    const { createApp } = Vue;
    createApp({
        data() {
            return {
            }
        },
        components: {
            AlertBox
        }
    }).mount('#app');
</script>

 

相关推荐

  1. vue学习笔记组合式API

    2024-02-05 11:56:02       49 阅读
  2. Vue学习笔记应用创建和基础知识

    2024-02-05 11:56:02       64 阅读
  3. Vue学习笔记-缓存路由组件

    2024-02-05 11:56:02       55 阅读
  4. Vue学习笔记侦听器

    2024-02-05 11:56:02       68 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-05 11:56:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-05 11:56:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-05 11:56:02       87 阅读
  4. Python语言-面向对象

    2024-02-05 11:56:02       96 阅读

热门阅读

  1. 可控概率抽奖算法

    2024-02-05 11:56:02       53 阅读
  2. PostgreSQL解决序列(自增id)自动增长冲突

    2024-02-05 11:56:02       50 阅读
  3. Python 处理小样本数据的文档分类问题

    2024-02-05 11:56:02       53 阅读
  4. 字符串工具类

    2024-02-05 11:56:02       40 阅读
  5. Chapter 8 - 6. Congestion Management in TCP Storage Networks

    2024-02-05 11:56:02       59 阅读
  6. 算法提升——LeetCode383场周赛总结

    2024-02-05 11:56:02       57 阅读
  7. 力扣_字符串3—通配符匹配

    2024-02-05 11:56:02       37 阅读
  8. prettier和eslint冲突怎么解决?

    2024-02-05 11:56:02       58 阅读