Vue.js develops a globally called MessageBox component

小云云
Release: 2017-12-06 16:30:56
Original
1730 people have browsed it

Preface

Currently when developing a personal blog project, I was not prepared to use some of the more popular UI libraries from the beginning (after all, it is a personal project, so I need to practice more. Okay), so you need to develop several global components yourself. Here we take MessageBox as an example to record how Vue.js develops global components.

Source code

Talk is cheap. Show me the code.

Component template

// /src/components/MessageBox/index.vue




Copy after login

Add global functions to components

vue.js official documentation has an introduction to developing plug-ins. The specific implementation code is as follows:

// /src/components/MessageBox/index.js

import msgboxVue from './index.vue';
// 定义插件对象
const MessageBox = {};
// vue的install方法,用于定义vue插件
MessageBox.install = function (Vue, options) {
const MessageBoxInstance = Vue.extend(msgboxVue);
let currentMsg;
const initInstance = () => {
// 实例化vue实例
currentMsg = new MessageBoxInstance();
let msgBoxEl = currentMsg.$mount().$el;
document.body.appendChild(msgBoxEl);
};
// 在Vue的原型上添加实例方法,以全局调用
Vue.prototype.$msgBox = {
showMsgBox (options) {
if (!currentMsg) {
initInstance();
}
if (typeof options === 'string') {
currentMsg.content = options;
} else if (typeof options === 'object') {
Object.assign(currentMsg, options);
}
return currentMsg.showMsgBox()
.then(val => {
currentMsg = null;
return Promise.resolve(val);
})
.catch(err => {
currentMsg = null;
return Promise.reject(err);
});
}
};
};
export default MessageBox;
Copy after login

Global use

// src/main.js
import MessageBox from './components/MessageBox/index';
Vue.use(MessageBox);
Copy after login

Page call

According to the previous definition A good method, you can happily call this component in various pages.

this.$msgBox.showMsgBox({
title: '添加分类',
content: '请填写分类名称',
isShowInput: true
}).then(async (val) => {
// ...
}).catch(() => {
// ...
});

Copy after login

The last rendering

Vue.js develops a globally called MessageBox component

##The above content is Vue. js to develop a globally called MessageBox component, I hope it will be helpful to everyone.

Related recommendations:

vue.js implementation method of parent passing parameters to child components

Detailed analysis of several postures in Vue.js component communication

Detailed learning of common instructions in Vue.js


The above is the detailed content of Vue.js develops a globally called MessageBox component. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!