關于iview按需引用后使用this.$Modal報錯的解決
iview按需引用后使用this.$Modal報錯
在做需求的時候,需要在點擊某處的時候出現(xiàn)一個警告框,于是想到使用iview官方文檔的所推薦的Modal對話框來創(chuàng)建一次性的輕量級對話框。
main.js中引入了iview
import { Button, Modal } from 'iview'
Vue.component('Button', Button)
Vue.component('Modal', Modal)錯誤信息如下

代碼如下:
<template>
<Button @click="instance('warning')">warning</Button>
<Button @click="instance('success')">Success</Button>
</template><script>
export default {
methods: {
instance (type) {
const title = 'Title';
const content = '<p>Content of dialog</p><p>Content of dialog</p>';
switch (type) {
case 'warning':
this.$Modal.warning({
title: title,
content: content
});
break;
case 'success':
this.$Modal.success({
title: title,
content: content
});
break;
}
}
}
}
</script>原因如下
引用:this.$Modal.warning()
結(jié)果:Uncaught (in promise) TypeError: Cannot read property 'warning' of undefined
原因:打印出來的this.$Modal也是undefined,說明沒有聲明$Modal
解決方法
在main.js中$Model聲明一下:
Vue.prototype.$Modal = Modal
iview中如何按需加載Moda
iview文檔:https://www.iviewui.com/components/modal
第一步使用modal組件,如何在我需要的時候在加載內(nèi)容?
初始值:isShow=false
使用v-if指令
? <div v-if="isShow"> ? ? ? ? <Modal v-model="addUser" ?title="創(chuàng)建用戶" > ? ? ? ? ? ? <add-user></add-user> ? ? ? ? </Modal> ? ? </div>
在使用時再讓isShow=true,這樣dom就會重新渲染
如何此時addUser=true的話,你會看不到動畫效果的,因為這存在一個異步
需要dom加載完成后操作
?const that = this;
?this.isShow = true;
? ? ?this.$nextTick(function () {
? ? ? ? ?that.addUser =true;
? ? ?})引入
const addUser =()=>import('xx.js');
components:{
? ? ? ? "add-user":addUser
? ? },還有一步:
output:{chunkFilename: 'js/[name].js',}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue是怎么渲染template內(nèi)的標簽內(nèi)容的
這篇文章主要介紹了Vue是怎么渲染template內(nèi)的標簽內(nèi)容的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Vue中router-view和component :is的區(qū)別解析
這篇文章主要介紹了Vue中router-view和component :is的區(qū)別解析,router-view用法直接填寫跳轉(zhuǎn)路由,路由組件會渲染<router-view></router-view>標簽,本文給大家介紹的非常詳細,需要的朋友可以參考下2023-10-10

