VueJS 插件开发问题

2016-10-04 15:32:31 +08:00
 ZGLHHH

我想把store.js整合,使其能通过Vue.localstorethis.$localstore调用

但是却报错:

插件代码如下:

import store from "store";

(function(){

    function install(Vue){
        Object.defineProperty(Vue.prototype, '$localstore',{
            get(){
                return {

                    get(key){
                        return store.get(key);
                    },

                    set(key,value){
                        return store.set(key,value);
                    },

                    remove(key){
                        return store.remove(key);
                    }
                }

            }
        });

    }


    if (typeof exports == "object") {
        module.exports = install
    } else if (typeof define == "function" && define.amd) {
        define([], function(){ return install })
    } else if (window.Vue) {
        Vue.use(install)
    }

})()

调用代码如下:

import VueStore from './plugins/localstore.js';
Vue.use(VueStore);
Vue.localstore.get('token');
2887 次点击
所在节点    Vue.js
8 条回复
ChiangDi
2016-10-04 15:59:04 +08:00
直接用原来的不好吗。。。都有模块系统了,为什么要把它挂在 Vue 下面??
ZGLHHH
2016-10-04 16:08:14 +08:00
@ChiangDi 那就要在每个组件都 import 一次
SourceMan
2016-10-04 16:39:12 +08:00
@ZGLHHH 每个组件都头部声明好依赖不是挺清晰的吗?
learnshare
2016-10-04 16:49:21 +08:00
@ZGLHHH 每个组件 import 一次,不正是组件化的好处么?
ZGLHHH
2016-10-04 16:54:01 +08:00
@learnshare
@SourceMan

呃,这样做不会令 js 文件变大吗?
SourceMan
2016-10-04 17:12:34 +08:00
@ZGLHHH 声明依赖并不是重复引用
learnshare
2016-10-04 18:04:09 +08:00
@SourceMan 是的,模块只引入一次,多处引用
ZGLHHH
2016-10-04 18:08:03 +08:00
@SourceMan
@learnshare

十分感谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/310519

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX