(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.js
export 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,
},
(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",
});
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> //显示
命名记得不要用到大写,html标签不吃小驼峰
动态资源
技巧:前内、後外
<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",
};
},
<props-validation :prop-a="fun"> </props-validation>
data() {
return {
fun: () => {
return "a";
},
};
},
app.component("props-validation", {
props: {
propA: Function,
// propA: String, //会跳警告但不会影响运作
}
$emit连接器
(传递)触发事件
内资料传外
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>
`,
});
制作元件
app.component("card", {
template:
<slot>
<p style="color: red">这是预设值</p>
</slot>
html替换 template /slot 内的内容
<card>
<p style="color: red">此处由外层定义YOOOOOO</p>
</card>
将内部的部分data资料给Slot,让外部得以抓取
(1)取出元件的值使用(一个)
(2)取出元件的值使用(多个)
不只Vue可使用,有类似需求都可以装此套件
https://github.com/developit/mitt
v-for="i in array" :key="i"
更改元件样式
.checkbox-text-color{
.v-label{
color:red;
}
}
<<: 课堂小考 - 深度学习 Deep Learning Q&A(2)
>>: 课堂笔记 - 深度学习 Deep Learning (17)
接下来就是跟资料库连结了,会着重说明 ASP.NET Core 跟 EF Core,如果有需要改画面...
在之前的文章我们建立好基本的Bot,也在Azure开好了Azure Bot资源 之後我们就可以将写好...
大家好! 相信各位都有看到标题了,今天开始就要实作函式库了。 我们进入今天的主题吧! jQuery ...
课程练习 新建完整的路由,并提供导览进入大部分页面(进入页面仅需要文字,不必加入实际的页面) 使用 ...
铁人赛最後一天! 今天要跟各位分享的呢,是昨天与前天 Banana in a box 系列的延伸,关...