第一種:通過組件來調用顯示的
<template>
<!--src/component/custom/main.vue-->
<div class = "contine">
<p class = "title">
{{title}}
</p>
<slot class = "contine" name = "contine">
</slot>
</div>
</template>
<script>
export default {
name:"test-component",
props:{
title:{
type : String,
default:"組件標題"
}
}
}
</script>
<style lang="scss" scoped>
$black : #333;
$mainSize : 16px;
$mainLineHeight:30px;
$lineCenter:center;
.extendStyle {
border-radius:$mainSize;
box-shadow: 3px 3px 3px 3px #333;
}
.contine{
width:50%;
height:130px;
color:$black;
font-size:$mainSize;
@extend .extendStyle;
.title,.contine{
color:$black;
font-size:$mainSize;
text-align:$lineCenter;
line-height:$mainLineHeight;
}
}
</style>
組件說明:只是一個簡單的組件 顯示一個標題和一段自定義slot內容(僅用作展示)篷朵;我想通過以下方式引入
import TestModel from "./testComponent/index";
Vue.use(TestModel);
并且調用方法為:
<test-conponent title = "123">
<div slot = "contine">
2222
</div>
</test-conponent>
這么做虽风,像不像element魂角?是有點這個意思對吧溢十,但是怎么實現(xiàn)呢诉植,在這先說明下use的作用届腐,其實就是相當于執(zhí)行他的install方法适篙,明白這個 之后 開始動工往核,思路:執(zhí)行他的install方法,聲明一個vue組件嚷节,組件內容就是上面寫的铆铆,從而達到全局組件的目的,來看看我的js怎么寫的:
import main from "./main";
main.install = (Vue) => {
Vue.component("test-conponent",main);
}
export default main;
沒錯就是這么一句丹喻,第一種的簡單組件就寫完了薄货,一次引入 全局通用。
第二種碍论、通過指令調用谅猾,比如element的loading組件等
<template>
<div class = "loading-container" v-show = "show">
<div class = "loading-mask"></div>
<div class="loading-content">
<div class="loading-animate"></div>
<div class="loading-text">
{{text}}
</div>
</div>
</div>
</template>
<script>
export default {
props:{
show:{
type:Boolean,
},
text:{
type:String,
default:"加載中...",
}
}
}
</script>
<style>
.loading-container {
position: relative;
text-align: center;
}
.loading-mask {
position:fixed;
top:0;
bottom:0;
left: 0;
right: 0;
background-color: rgba(0,0,0,.7);
}
.loading-content{
position:fixed;
left:50%;
top:45%;
z-index:999;
transform: translate(-50%,-45%);
text-align:center;
color:#fff;
}
.loading-content .loading-animate{
display:inline-block;
width:40px;
height:40px;
vertical-align: baseline;
margin:25px 0 10px;
vertical-align: middle;
animation:cricleLoading 1s steps(12,end) infinite;
background: transparent url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMjAiIGhlaWdodD0iMTIwIiB2aWV3Qm94PSIwIDAgMTAwIDEwMCI+PHBhdGggZmlsbD0ibm9uZSIgZD0iTTAgMGgxMDB2MTAwSDB6Ii8+PHJlY3Qgd2lkdGg9IjciIGhlaWdodD0iMjAiIHg9IjQ2LjUiIHk9IjQwIiBmaWxsPSIjRTlFOUU5IiByeD0iNSIgcnk9IjUiIHRyYW5zZm9ybT0idHJhbnNsYXRlKDAgLTMwKSIvPjxyZWN0IHdpZHRoPSI3IiBoZWlnaHQ9IjIwIiB4PSI0Ni41IiB5PSI0MCIgZmlsbD0iIzk4OTY5NyIgcng9IjUiIHJ5PSI1IiB0cmFuc2Zvcm09InJvdGF0ZSgzMCAxMDUuOTggNjUpIi8+PHJlY3Qgd2lkdGg9IjciIGhlaWdodD0iMjAiIHg9IjQ2LjUiIHk9IjQwIiBmaWxsPSIjOUI5OTlBIiByeD0iNSIgcnk9IjUiIHRyYW5zZm9ybT0icm90YXRlKDYwIDc1Ljk4IDY1KSIvPjxyZWN0IHdpZHRoPSI3IiBoZWlnaHQ9IjIwIiB4PSI0Ni41IiB5PSI0MCIgZmlsbD0iI0EzQTFBMiIgcng9IjUiIHJ5PSI1IiB0cmFuc2Zvcm09InJvdGF0ZSg5MCA2NSA2NSkiLz48cmVjdCB3aWR0aD0iNyIgaGVpZ2h0PSIyMCIgeD0iNDYuNSIgeT0iNDAiIGZpbGw9IiNBQkE5QUEiIHJ4PSI1IiByeT0iNSIgdHJhbnNmb3JtPSJyb3RhdGUoMTIwIDU4LjY2IDY1KSIvPjxyZWN0IHdpZHRoPSI3Ii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keyframes cricleLoading{
0%{
transform: rotate3d(0,0,1,0deg);
}
100%{
transform: rotate3d(0,0,1,360deg);
}
}
@keyframes cricleLoading{
0%{
transform: rotate3d(0,0,1,0deg);
}
100%{
transform: rotate3d(0,0,1,360deg);
}
}
</style>
以上是loading組件的內容,要通過這種方式引入:
import loading from "./toolTip";
Vue.use(loading);
并且通過指令方式調用:
this.$loading.show({
text:"拼命加載中"
});
有的朋友可能會說了,這怎么可能啊税娜,沒有節(jié)點怎么放進去坐搔?對 沒錯 確實,沒有節(jié)點是沒辦法進去的敬矩,但是沒有節(jié)點就聲明節(jié)點 對么概行?
import LoadingComponent from "./loading";
import Vue from "vue";
let instance;
const loadingConstructor = Vue.extend(LoadingComponent);
instance = new loadingConstructor({
el:document.createElement("div")
});
instance.show = false;
const loading = {
show(options = {}){
instance.show = true;
document.body.appendChild(instance.$el);
instance.text = options.text;
setTimeout(()=>{
loading.hide();
},5000)
},
hide() {
instance.show = false;
}
}
export default {
install(){
if(!Vue.loading){
Vue.$loading = loading;
}
Vue.mixin({
created(){
this.$loading = Vue.$loading;
}
})
}
}
實際上就是變相的利用install方法 吧loading指令掛到頁面中,這樣不管在哪里都可以直接用this.$loading調用弧岳。
get到了么凳忙?get到了就點個關注吧