文档 / 组件开发

如何在框架中使用vue和vux组件

要在light框架中使用vue组件,首先需要在命令行安装vue插件light plugin -a vue,page 页面中需要引用js 文件vue-components.js(启用时用light release -wb -e vue)。

<!DOCTYPE html>
<html>
<head>
<snippet id="header" />
<!-- build:css css/index.css -->
<link rel="stylesheet" href="css/style.css" />
<!--<link rel="stylesheet" href="css/index.css" />-->
<!-- endbuild -->
<title>lightProjectDemo-Just For Fun</title>
</head>
<body>
<div id="main">
<view id="lightProjectDemo" home="true" />
</div>
<snippet id="footer" />
<script type="text/javascript" src="//res.lightyy.com/lighting/framework/light-2.2.min.js"></script>
<script type="text/javascript" src="config.js"></script>
<!-- build:js js/index.js -->
<script src="vue-components.js"></script>
<!--<script type="text/javascript" src="js/lib/underscore.js"></script>-->
<!-- inject:view -->
<!-- endinject -->
<!-- endbuild -->
<script type="text/javascript">
Light.start();
</script>
</body>
</html>

做好这些准备工作后,我们就可以使用vue组件了,假如我们现在需要做一个头部的组件head.vue,步骤如下:

1.在目录结构的vue文件夹中新建一个vue文件命名为head.vue

<template>
<div class="headFixed">
<h2 class="tab">
<span @click="back()" class="back">返回</span>
<em class="view">{{title}}</em>
</h2>
</div>
</template>
<style>
</style>
<script>
export default {
props:['title'],
methods:{
back:function () {
history.back();
}
}
}
</script>

2.调用

在视图中的调用方法如下:

<div id="lightProjectDemo" style="display:none;">
<vue-head :title.sync="title"></vue-head>
</div>

这样一个vue组件就调用成功了,title的值可以在视图的js文件中根据需要设置。

;(function(){
App.defineViewModel("#lightProjectDemo",{
data:{
title:"首页"
},
watch:{},
methods:{}
},{
ready:function(){
},
beforeRender:function (params) {
},
afterRender:function (params){
},
beforeUnRender:function (){
},
afterUnRender:function (){
}
});
})();

vux组件 的使用方法同上,假如我们现在需要使用vux组件中的五星评价功能,先新建一个vue文件命名为star.vue,然后把vux的对应的代码拿过来

<template>
<div class="vux-rater">
<input v-model="currentValue" style="display:none">
<a class="vux-rater-box" v-for="i in max" @click="handleClick(i-1)" :class="{'is-active':currentValue > i-1}" :style="{color: colors && colors[i-1] ? colors[i-1] : '#ccc',marginRight:margin+'px',fontSize: fontSize + 'px', width: fontSize + 'px', height: fontSize + 'px', lineHeight: fontSize + 'px'}">
<span class="vux-rater-inner">{{star}}<span class="vux-rater-outer" :style="{color: activeColor, width: cutPercent + '%'}" v-if="cutPercent > 0 && cutIndex === i-1">{{star}}</span></span>
</a>
</div>
</template>
<script>
export default {
created () {
this.currentValue = this.value
},
mounted () {
this.updateStyle()
},
props: {
max: {
type: Number,
default: 5
},
value: {
type: Number,
default: 0
},
disabled: Boolean,
star: {
type: String,
default: '★'
},
activeColor: {
type: String,
default: '#fc6'
},
margin: {
type: Number,
default: 2
},
fontSize: {
type: Number,
default: 25
}
},
computed: {
sliceValue () {
const _val = this.currentValue.toString().split('.')
return _val.length === 1 ? [_val[0], 0] : _val
},
cutIndex () {
return this.sliceValue[0] * 1
},
cutPercent () {
return this.sliceValue[1] * 10
}
},
methods: {
handleClick (i, force) {
if (!this.disabled || force) {
if (this.currentValue === i + 1) {
this.currentValue = i
this.updateStyle()
} else {
this.currentValue = i + 1
}
}
},
updateStyle () {
for (var j = 0; j < this.max; j++) {
if (j <= this.currentValue - 1) {
this.$set(this.colors, j, this.activeColor)
} else {
this.$set(this.colors, j, '#ccc')
}
}
}
},
data () {
return {
colors: [],
currentValue: 0
}
},
watch: {
currentValue (val) {
this.updateStyle()
this.$emit('input', val)
},
value (val) {
this.currentValue = val
}
}
}
</script>
<style>
.vux-rater {
text-align: left;
display: inline-block;
line-height: normal;
}
.vux-rater a {
display: inline-block;
text-align: center;
cursor: pointer;
color: #ccc;
}
.vux-rater a:last-child {
padding-right: 2px!important;
margin-right: 0px!important;
}
.vux-rater a:hover {
color: #ffdd99;
}
.vux-rater a.is-disabled {
color: #ccc !important;
cursor: not-allowed;
}
.vux-rater-box {
position: relative;
}
.vux-rater-inner {
position: relative;
display: inline-block;
}
.vux-rater-outer {
position: absolute;
left: 0;
top: 0;
display: inline-block;
overflow: hidden;
}
</style>

最后在视图中调用即可

<div id="lightProjectDemo" style="display:none;">
<vue-head :title.sync="title"></vue-head>
<vue-star></vue-star>
</div>