Vue3 ( 元件 / components ) -2

1.元件注册方法有三,注册后才能使用

(1)全域注册
方法1.

          <alert></alert>
         // 1.串接跟元件,元件注册,名称为alert            .component("alert", {              data() {                return {                  text: "内部文字",                };              },              // 元件样板              template: `<div class="alert alert-primary" role="alert">              {{ text }}            </div>`,            });

方法2.

            <alert2></alert2>
            // 2.元件注册,名称为alert2            app.component("alert2", {              data() {                return {                  text: "元件 2",                };              },              // 元件样板              template: `<div class="alert alert-primary" role="alert">              {{ text }}            </div>`,            });

(2)区域注册
方法1.

            const alert3 = {              data() {                return {                  text: "元件 3",                };              },              // 元件样板              template: `<div class="alert alert-primary" role="alert">              {{ text }}            </div>`,            };

根元件增加components

             const app = Vue.createApp({              data() {                return {                  text: "外部元件文字",                };              },              // 3.显示-方法1 区域注册              components: {                alert3,              },
            <alert3></alert3>

方法2.放在元件2下面,便于管理

            const alert3 = {              data() {                return {                  text: "元件 3",                };              },              // 元件样板              template: `<div class="alert alert-primary" role="alert">              {{ text }}            </div>`,            };            app.component("alert2", {              data() {                return {                  text: "元件 2",                };              },              // 3.显示-方法2 区域注册              components: {                alert3,              },              // 元件样板              template: `<div class="alert alert-primary" role="alert">              {{ text }}              // 3.显示-方法2              <alert3></alert3>            </div>`,            });

(3)模组化

component-alert.jsexport default{    data() {      return {        text: "外部汇入的元件",      };    },    // 元件样板    template: `<div class="alert alert-primary" role="alert">    {{ text }}  </div>`,};
            <alert4></alert4>

引入

            import alert4 from "./alert-component.js";

到跟元素注册

            const app = Vue.createApp({              data() {                return {                  text: "外部元件文字",                };              },              components: {                // 4.显示 注册                alert4,              },

2.元件样板

(1)template

            <alert></alert>
            app.component("alert", {              template: `<div class="alert alert-primary" role="alert">               範例一               </div>`,            });

(2)x-template

    <script type="text/x-template" id="alert-template">      <!-- template格式 -->      <div class="alert alert-primary" role="alert">        x-template 所建立的元件        </div>    </script>
            <alert2></alert2>
            app.component("alert2", {              // 绑id              template: "#alert-template",            });

3.运用

v-is

字串

            app.component("alert", {              template: `<div class="alert alert-primary" role="alert">               範例一               </div>`,            });
            <div v-is="'alert'"></div>

变数

              data() {                return {                  array: [1, 2, 3],                  // "alert"元件名称,仓库                  componentName: "alert",                };              },             <input type="text" v-model="componentName" />  //抓
             <div v-is="componentName"></div>  //显示

4.Props 传送资料 (外传内)

命名记得不要用到大写,html标籤不吃小驼峰
http://img2.58codes.com/2024/2013768435axLlBGCO.png

动态资源
技巧:前内、后外

 <photo v-bind:url="imgUrl"></photo>

前内 url

            app.component("photo", {              // 1.Props 静态资料 定义 props属性(定义图片名称)              // 2.动态资源 定义 props属性(定义图片名称)后              props: ["url"],              template: `<img :src="url" class="img-thumbnail" alt>`,            });

后外 imgUrl

              data() {                return {                  imgUrl:                    "https://images.unsplash.com/photo-1605784401368-5af1d9d6c4dc?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=80",                };              },

5.Props 型别验证 要用{}

            <props-validation :prop-a="fun"> </props-validation>               data() {                return {                  fun: () => {                    return "a";                  },                };              },
                app.component("props-validation", {                props: {                propA: Function,                // propA: String,  //会跳警告但不会影响运作                }

6.Emit 内传外 or (传递)触发事件

$emit连接器

(传递)触发事件
http://img2.58codes.com/2024/20137684Q56AHkRYiq.jpg
内资料传外
http://img2.58codes.com/2024/20137684tRbOMNiA3b.png
http://img2.58codes.com/2024/20137684juKpuLLPO3.png
http://img2.58codes.com/2024/2013768489c9oHwox7.png


7.Emit传值 & 验证

http://img2.58codes.com/2024/20137684aSmXuEOPnX.png

1.内向外传值,可以直接写在html内(减略前述methods)
1-1.追蹤 emits: ["add"]

              data() {                return {                  num: 1,                };              },              // add事件追蹤[],并且可验证值{}              emits: ["add"],      template: `      <button type="button" @click="num++">调整 num 的值</button>        <button       v-on:click="$emit('add',num)"   //连接add,传num出去      type="button">add</button>`,            });

2.验证 emits:{}

            app.component("button-counter2", {              // add事件追蹤[],并且可验证值{}              emits: {                add: (num) => {                  if (typeof num != "number") {                    console.warn("add 事件参数型别须为number");                  }                  return typeof num === "number"; //1                },              },              template: `      3-1 点击按钮,$emit先执行内部add: (num) => 传入值 1 并 连接@add及传入值 1       <br>      <button type="button" @click="$emit('add', '1')">Emit 验证是否为数值</button>    `,      });

8.Slot 元件插槽

http://img2.58codes.com/2024/20137684wGQeMgGzcj.png
http://img2.58codes.com/2024/20137684iH5Gkmm3YW.png
http://img2.58codes.com/2024/20137684cXVW5nsFLH.png
製作元件

            app.component("card", {              template:            <slot>            <p style="color: red">这是预设值</p>            </slot>

html替换 template /slot 内的内容

            <card>              <p style="color: red">此处由外层定义YOOOOOO</p>            </card>

9.Slot props

将内部的部分data资料给Slot,让外部得以抓取
http://img2.58codes.com/2024/20137684bLfQwS6byx.png
http://img2.58codes.com/2024/20137684k9A70KphwA.png
(1)取出元件的值使用(一个)
http://img2.58codes.com/2024/2013768481sLBMs3Wq.png
(2)取出元件的值使用(多个)
http://img2.58codes.com/2024/20137684vVmn8YHtWb.png


10.Mitt套件 跨元件传递

不只Vue可使用,有类似需求都可以装此套件
https://github.com/developit/mitt
http://img2.58codes.com/2024/20137684OPN8GYrnxK.png
http://img2.58codes.com/2024/20137684SjAtX2fGyf.png


11.v-for必加key

v-for="i in array" :key="i" 

SPA 单页式应用程式

props > 接收变数( data内 + v-bind )

emits > function接收 (v-on)


更改元件样式

1. 将别人的元件新增 class='checkbox-text-color'

2. F12查看该样式 class v-label

.checkbox-text-color{  .v-label{    color:red;      }}

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章