[Improvement] Toast: support Vue.use to register (#690)

This commit is contained in:
neverland 2018-03-14 11:47:46 +08:00 committed by GitHub
parent 8f2bd93786
commit 461b6732eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 18 deletions

View File

@ -8,10 +8,7 @@ const tips = '// This file is auto gererated by build/bin/build-entry.js';
function buildVantEntry() { function buildVantEntry() {
const uninstallComponents = [ const uninstallComponents = [
'Lazyload', 'Lazyload',
'Waterfall', 'Waterfall'
'Toast',
'ImagePreview',
'Locale'
]; ];
const importList = Components.map(name => `import ${uppercamelize(name)} from './${name}';`); const importList = Components.map(name => `import ${uppercamelize(name)} from './${name}';`);

View File

@ -87,8 +87,6 @@ export default {
<style lang="postcss"> <style lang="postcss">
.demo-popup { .demo-popup {
height: 10000px;
.van-button { .van-button {
margin: 10px 0 10px 15px; margin: 10px 0 10px 15px;
} }

View File

@ -69,7 +69,7 @@ Dialog.resetDefaultOptions = () => {
}; };
Dialog.install = () => { Dialog.install = () => {
Vue.component(VanDialog.name, VanDialog); Vue.use(VanDialog);
}; };
Vue.prototype.$dialog = Dialog; Vue.prototype.$dialog = Dialog;

View File

@ -1,18 +1,16 @@
import Vue from 'vue'; import Vue from 'vue';
import ImagePreview from './image-preview'; import VueImagePreview from './image-preview';
let instance; let instance;
const ImagePreviewConstructor = Vue.extend(ImagePreview);
const initInstance = () => { const initInstance = () => {
instance = new ImagePreviewConstructor({ instance = new (Vue.extend(VueImagePreview))({
el: document.createElement('div') el: document.createElement('div')
}); });
document.body.appendChild(instance.$el); document.body.appendChild(instance.$el);
}; };
const ImagePreviewBox = (images, startPosition = 0) => { const ImagePreview = (images, startPosition = 0) => {
if (!instance) { if (!instance) {
initInstance(); initInstance();
} }
@ -27,4 +25,8 @@ const ImagePreviewBox = (images, startPosition = 0) => {
return instance; return instance;
}; };
export default ImagePreviewBox; ImagePreview.install = () => {
Vue.use(VueImagePreview);
};
export default ImagePreview;

View File

@ -97,8 +97,10 @@ const components = [
GoodsActionBigBtn, GoodsActionBigBtn,
GoodsActionMiniBtn, GoodsActionMiniBtn,
Icon, Icon,
ImagePreview,
List, List,
Loading, Loading,
Locale,
NavBar, NavBar,
NoticeBar, NoticeBar,
NumberKeyboard, NumberKeyboard,
@ -127,6 +129,7 @@ const components = [
TabbarItem, TabbarItem,
Tabs, Tabs,
Tag, Tag,
Toast,
TreeSelect, TreeSelect,
Uploader Uploader
]; ];

View File

@ -5,7 +5,10 @@ import defaultMessages from './lang/zh-CN';
const proto = Vue.prototype; const proto = Vue.prototype;
const defaultLang = 'zh-CN'; const defaultLang = 'zh-CN';
const locale = { const locale = {
init() { install() {
if (proto.$vantLang) {
return;
}
Vue.util.defineReactive(proto, '$vantLang', defaultLang); Vue.util.defineReactive(proto, '$vantLang', defaultLang);
Vue.util.defineReactive(proto, '$vantMessages', { [defaultLang]: defaultMessages }); Vue.util.defineReactive(proto, '$vantMessages', { [defaultLang]: defaultMessages });
}, },
@ -20,5 +23,6 @@ const locale = {
} }
}; };
locale.init(); locale.install();
export default locale; export default locale;

View File

@ -86,6 +86,10 @@ Toast.allowMultiple = (allow = true) => {
singleton = !allow; singleton = !allow;
}; };
Toast.install = () => {
Vue.use(VueToast);
};
Vue.prototype.$toast = Toast; Vue.prototype.$toast = Toast;
export default Toast; export default Toast;

View File

@ -1,6 +1,6 @@
import Waterfall from './directive.js'; import Waterfall from './directive.js';
const install = function(Vue) { Waterfall.install = function(Vue) {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
console.warn('[Vant warn] Waterfall is deprecated, please use List component instread.'); console.warn('[Vant warn] Waterfall is deprecated, please use List component instread.');
} }
@ -8,5 +8,4 @@ const install = function(Vue) {
Vue.directive('WaterfallUpper', Waterfall('upper')); Vue.directive('WaterfallUpper', Waterfall('upper'));
}; };
Waterfall.install = install;
export default Waterfall; export default Waterfall;

View File

@ -60,7 +60,7 @@ describe('Dialog', () => {
expect(Dialog.currentOptions.title).to.equal(''); expect(Dialog.currentOptions.title).to.equal('');
}); });
it('register dialog component', () => { it('register component', () => {
Vue.use(Dialog); Vue.use(Dialog);
expect(!!Vue.component('van-dialog')).to.be.true; expect(!!Vue.component('van-dialog')).to.be.true;
}); });

View File

@ -54,4 +54,9 @@ describe('ImagePreview', () => {
done(); done();
}); });
}); });
it('register component', () => {
Vue.use(ImagePreview);
expect(!!Vue.component('van-image-preview')).to.be.true;
});
}); });

View File

@ -1,3 +1,4 @@
import Vue from 'vue';
import Toast from 'packages/toast'; import Toast from 'packages/toast';
describe('Toast', () => { describe('Toast', () => {
@ -132,4 +133,9 @@ describe('Toast', () => {
const toast2 = Toast(1); const toast2 = Toast(1);
expect(toast2.duration).to.equal(3000); expect(toast2.duration).to.equal(3000);
}); });
it('register component', () => {
Vue.use(Toast);
expect(!!Vue.component('van-toast')).to.be.true;
});
}); });