const app = Vue . createApp ({ /* ... */ }). mount ( "#app" );app . config . compilerOptions . isCustomElement = tag =>tag . startsWith ( 'ion-' );
- "stylesheet" href="../../assets/font/iconfont.css">
- <style>
- body{
- padding-bottom: 800px;
- }
- .iconfont{
- font-size: 30px;
- }
- icon-message,icon-set,collection{
- font-family: "iconfont" !important;
- font-size: 30px;
- }
- icon-message::before{
- content: "\e77e";
- }
- icon-set::before{
- content: "\e78e";
- }
- collection::before{
- content: "\e60c";
- }
- style>
- <p class="iconfont icon-email-fill">p>
- <p class="iconfont">p>
- <icon-message>icon-message>
-
- <div id="app">
- <h4>渲染自定义标签指定的图标h4>
- <p class="iconfont icon-email-fill">p>
- <p class="iconfont">p>
- <icon-message>icon-message>
- <icon-set>icon-set>
- <collection>collection>
- div>
-
- <script type="module">
- import { createApp } from "../../assets/vue/3.0/vue.esm-browser.js";
- const customTags = ["icon-message","icon-set","collection"]
-
- const app = createApp({
- data(){
- return {
-
- }
- }
- })
-
- // 通过扩展自定义组件标签的判断逻辑,描述那些自定义标签不是组件
- app.config.compilerOptions.isCustomElement = function(tag){
- // tag 参数 为程序运行时所有自定义标签的名称
- console.log(tag);
- // 通过返回 true 表示标签为自定义标签
- // 通过返回 false 表示标签为组件标签
- // return tag=="icon-message"
- // return tag.startsWith("icon-")
- return customTags.includes(tag);
- }
-
- app.mount("#app")
- script>
+ 参数
- plugin : Object|Function
Object = { install:function(app [,options] ){} }
Function = function(app [,options] ){}
* 方法对应的第一个参数app为调用时的应用对象(createApp构建对象)
* options 在插件加载过程中提供额外的配置参数
// plugins 定义export default {install : function ( app , options ){/*app: 当前安装插件的 Vue 应用程序实例对象,通过该对象可以扩展全局配置等相关功能options: 当前安装插件时提供的额外参数数据*/console . log ( "app:" , app );console . log ( "options:" , options );}}// 或者export default function ( app , options ){console . log ( "app:" , app );console . log ( "options:" , options );}
局部组件
- <div id="app">
-
- div>
-
- <script type="module">
- import { createApp } from "../../assets/vue/3.0/vue.esm-browser.js";
- const app = createApp({
- data(){
- return {
-
- }
- }
- })
- // 插件1
- let plugin1 = {
- install:function(app,opt){
- console.log("插件1:",app,opt);
- }
- }
- app.use(plugin1,"额外参数")
- // 插件2
- let plugin2 = function(app,opt){
- console.log("插件2:",app,opt);
- }
- app.use(plugin2,{
- name:"额外参数"
- })
- app.mount("#app")
- script>
全局组件
- "stylesheet" href="../../assets/font/iconfont.css">
- <style>
- body{
- padding-bottom: 800px;
- }
- .iconfont{
- font-size: 30px;
- }
- icon-message,icon-set,collection{
- font-family: "iconfont" !important;
- font-size: 30px;
- }
- icon-message::before{
- content: "\e77e";
- }
- icon-set::before{
- content: "\e78e";
- }
- collection::before{
- content: "\e60c";
- }
- style>
-
-
{{ $ajaxBase }}
-
-
-
-
-
全局指令
-
-
-
-
-
- import { createApp } from "../../assets/vue/3.0/vue.esm-browser.js";
- import pluginA from "./plugins/pluginA.js"
- import directives from "./plugins/directives.js"
- import components from "./plugins/components.js"
-
- // const app = createApp({
- // data(){
- // return {
-
- // }
- // }
- // })
- // app.use(pluginA,["icon-message"])
- // app.mount("#app")
-
- // vue3借鉴了Jquery的链式语法规则
- createApp({
- data(){
- return {
-
- }
- }
- }).use(pluginA,["icon-message"])
- .use(directives)
- .use(components)
- .mount("#app")
-
- .box{
- margin: 10px;
- border: 1px solid #999;
- min-height: 100px;
- padding: 10px;
- }
-
-
-
父组件
-
msg:{{ msg }}
-
-
-
- v-bind:info="msg"
- :num="num"
- :arr="arr"
- :user="user"
- text="测试组件属性的静态绑定"
- :flag="false"
- >
-
-
-
-
-
-
-
子组件-CompA
-
$attrs.info:{{ $attrs.info }}
-
info:{{ info }}
-
-
flag:{{ flag }}
-
flag值为true显示
-
-
-
-
- import { createApp } from "../../assets/vue/3.0/vue.esm-browser.js";
- const app = createApp({
- data(){
- return {
- msg:"父组件数据变量-msg",
- num:10,
- num1:11,
- arr:[1,2,3,4],
- user:{
- name:"tom"
- }
- }
- }
- })
-
- app.component("CompA",{
- template:"#compA",
- props:["info","num","arr","user","text","flag"],
- beforeCreate() {
- console.log("this.$attrs:",this.$attrs)
- },
- created() {
- console.log("this.$attrs:",this.$attrs)
- },
- })
-
- app.mount("#app")
-
特殊属性处理和属性穿透
-
- body {
- padding-bottom: 800px;
- }
-
- .fc {
- color: red;
- }
-
- .box {
- margin: 10px;
- border: 1px solid #999;
- min-height: 100px;
- padding: 10px;
- }
-
-
-
-
父组件
-
-
-
-
-
-
-
-
-
-
子组件-CompA-1
-
-
-
子组件-CompA-2
-
-
-
-
- import { createApp } from "../../assets/vue/3.0/vue.esm-browser.js";
- const app = createApp({
- data(){
- return {
- classStr:"fc"
- }
- }
- })
-
- app.component("CompA",{
- template:"#compA",
- // props:["class","style"]
- props:["a"]
- })
-
- app.mount("#app")
props的数据校验
/*
props 的数据校验不是给用户提供错误提示的
props 的数据校验是为开发者提供 合理数据 判断
所以通过props的对象取值方案,就可以实现数据传入组件时,完成必要数据校验,以提供调用提示
props:{
key:value,
……
}
+ key 被拦截的属性名
+ value 构造函数|Object对象配置
-> 构造函数 = 描述当前拦截的属性只能接收某种数据类型的值或者某几种数据类型的值
-> Object对象配置 =
*/
-
- body {
- padding-bottom: 800px;
- }
-
- .box {
- margin: 10px;
- border: 1px solid #999;
- min-height: 100px;
- padding: 10px;
- }
-
-
- <div id="app">
- <h4>父组件h4>
- <p>num:{{ num}} - {{ typeof(num) }}p>
- <comp-a :source="num" :msg=" info ">comp-a>
- div>
-
- <script type="text/x-template" id="compA">
- <div class="box">
- <h4>子组件-CompAh4>
- <p>评分:{{ source.toFixed(1) }}p>
- <p>msg:{{ msg }}p>
- div>
- script>
-
- <script type="module">
- import { createApp } from "../../assets/vue/3.0/vue.esm-browser.js";
- const app = createApp({
- data(){
- return {
- num:8,
- // num:"7",
-
- // info:"测试类型"
- // info:100
- // info:false
- info:[1,2,3]
- }
- }
- })
-
- app.component("CompA",{
- template:"#compA",
- // props:["source"]
- props:{
- source:Number,
- msg:[String,Number,Boolean]
- }
- })
-
- app.mount("#app")
-
- script>