2019-04-09
1.搭建vue项目
2. 页面构成及视图等
3. 如何引入jquery
2019-04-10
4.引入ant-designUI
5.路由按需加载
6.Eslint检查
2019-04-11
7.v-for遍历一个div
…待整理
2019-04-26
8.获取下拉列表数据,调用后台接口
以下是回答 ↓↓
问题1-搭建vue项目
环境搭建node-淘宝镜像-webpack-vue-cli- vue -V
- 在你要创建的位置按住shift+鼠标右键,在此处打开命令窗口,创建项目指令
- vue init webpack project(项目名)
- 运行项目
- npm run dev
- 打开浏览器8080端口
问题2-页面构成及视图等
- App.vue 上主要写页面/系统中固定好的一部分,如导航栏等
- mian.js 上主要写引入的文件,包括引入的UI组件等
- index.js 上是生成的路由,具体的子组件也需要在这里import
页面的每一个组件都是如下结构
<template>
<div></div>
</template>
导航的链接方式,使用router-link
<router-link to="./index">首页</router-link>
子组件显示视图
<router-view></router-view>
问题3-如何引入jquery
(1)npm install jquery --save-dev
(2)在webpack.base.conf.js文件中引入以下
var webpack=require('webpack');
plugins:[
new webpack.ProvidePlugin({
$:"jquery",
jQuery:"jquery",
"windows.jQuery":"jquery"
})
]
(3)在具体的页面中引入
<p class="a"></p>
import $ from 'jquery'
$(function(){
$('.a').click(function(){
alert('JQ引入测试');
});
});
问题4-引入ant-designUI
引入ant-design(参考:https://vue.ant.design/docs/vue/getting-started-cn/)
npm i --save ant-design-vue
可以考虑UI组件按需加载,也可以引入整个css
(1)UI组件按需加载
在main.js中引入
import { Button } from 'ant-design-vue';
Vue.use(Button);
(2)全局加载UI样式
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
如果发现有时引入的样式显示异常,那可能就是全局的样式影响到了,检查一下当前页面style scoped之类即可
**推荐路径自动补全插件
Path Intellisense
问题5-路由按需加载
export default new Router({
routes : [
{
path:'/',name:'index',component:resolve => require(['../components/index.vue'],resolve),
children:[
{
path:'/user',name:'user',component:resolve => require(['../components/user.vue'],resolve),
}
]
}
]
})
问题6-Eslint检查
遍历div用到v-for循环时,出现了Eslint检查,必须使用v-bind绑定key值,可在设置中关闭检查
setting中将vetur.validation.template 改为false
问题7-v-for遍历一个div
<div v-for="i in arr">
{{i.name}}
</div>
如果遇到其中有某UI自身属性要设置时,则需要将属性变为绑定值、
具体语法是在其前面加:冒号
<div v-bind:title = "title">hello</div>
//此处的title绑定下面的属性时,要用v-bind指令,js表达式
data:{
title:"this is the title"
}
简写为:<div :title = "title">hello</div>
应用:
<a-card-meta :title="i.name" :description="i.type">
<a-avatar
slot="avatar"
src="pic1.png"
/>
</a-card-meta>
问题8-获取下拉列表数据,调用后台接口:
<a-form-item
v-bind="twoColumnFormItemLayout" //绑定栅格为两列显示
label="民族" //label名称
has-feedback> //用于给输入框添加反馈图标
<a-select
showSearch //提供输入检索匹配
v-decorator="[ //验证
'select',
{rules: [{ required: true, message: '请选择民族!' }]}]"
placeholder="请选择民族!"
mode="multiple"> //表示多选
<a-select-option v-for="n in nation" >{{n.nation}}</a-select-option>
//绑定数据源,value属性绑定
</a-select>
</a-form-item>
const nation = [];
export default {
data () {
return {
nation,
twoColumnFormItemLayout: {
labelCol: {
xs: { span: 24 },
sm: { span: 6 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 16 },
},
},
},
mounted () {
//查询民族
this.$ajax.get('http://..../queryNation').then( res => {
this.nation = res.data;
})
}
}