@click="handleChange(value)">
<span :class="{closed: !checked}"></span>
</div>
<input
type="checkbox"
@change="handleChange"
:true-value="activeValue"
:false-value="inactiveValue"
:disabled="disabled"
:value="value"/>
</div>
</div>
</template>
<script>
export default {
name: "UniSwitch",
data() {
return {}
},
props: {
value: {
type: [Boolean, String, Number],
default: false
},
activeValue: {
type: [Boolean, String, Number],
default: true
},
inactiveValue: {
type: [Boolean, String, Number],
default: false
},
disabled: {
type: Boolean,
default: false
}
},
computed: {
checked() {
return this.value === this.activeValue;
}
},
methods: {
handleChange(value) {
this.$emit('input', !this.checked ? this.activeValue : this.inactiveValue);
}
}
}
</script>
index.js:
// UniSwitch 是对应组件的名字,要记得在 moor-switch.vue 文件中还是 name 属性哦
import UniSwitch from './UniSwitch.vue';
UniSwitch.install = Vue => Vue.component(UniSwitch.name, UniSwitch);
export default UniSwitch;好了基本完成了,但是为了将所有的组件集中起来比如我还有 select、 input、 button 等组件,那么我想要统一将他们放在一个文件这中便于管理
所以在 App.vue 同级目录我新建了一个 index.js 文件
import UniSwitch from './packages/switch/index';
import UniSlider from './packages/slider/index';
import UniNumberGrow from './packages/number-grow/index';
import './common/scss/reset.css'
// ...如果还有的话继续添加const components = [
UniSwitch,
UniSlider,
UniNumberGrow
// ...如果还有的话继续添加
]
const install = function (Vue, opts = {}) {
components.map(component => {
Vue.component(component.name, component);
})
}
/* 支持使用标签的方式引入 */
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
export default {
install,
UniSwitch,
UniSlider,
UniNumberGrow
// ...如果还有的话继续添加
}
好了到这里我们的组件就开发完成了;下面开始说怎么打包发布到 npm 上
发布到 npm
打包之前,首先我们需要改一下 webpack.config.js 这个文件;
// ... 此处省略代码
const NODE_ENV = process.env.NODE_ENV
module.exports = {
// 根据不同的执行环境配置不同的入口
entry: NODE_ENV == 'development' ? './src/main.js' : './src/index.js',










