vue 学习笔记第无弹
1. 在 webpack 中配置 .vue 组件页面的解析
运行
cnpm i vue -S
将 vue 安装为运行依赖;运行
cnpm i vue-loader vue-template-compiler -D
将解析转换 vue 的包安装为开发依赖;运行
cnpm i style-loader css-loader -D
将解析转换 CSS 的包安装为开发依赖,因为 .vue 文件中会写 CSS 样式;在
webpack.config.js
中,添加如下module
规则:
1module: {
2
3 rules: [
4
5 { test: /\.css$/, use: ['style-loader', 'css-loader'] },
6
7 { test: /\.vue$/, use: 'vue-loader' }
8
9 ]
10
11 }
创建
App.js
组件页面:
1 <template>
2
3 <!-- 注意:在 .vue 的组件中,template 中必须有且只有唯一的根元素进行包裹,一般都用 div 当作唯一的根元素 -->
4
5 <div>
6
7 <h1>这是APP组件 - {{msg}}</h1>
8
9 <h3>我是h3</h3>
10
11 </div>
12
13 </template>
14
15
16
17 <script>
18
19 // 注意:在 .vue 的组件中,通过 script 标签来定义组件的行为,需要使用 ES6 中提供的 export default 方式,导出一个vue实例对象
20
21 export default {
22
23 data() {
24
25 return {
26
27 msg: 'OK'
28
29 }
30
31 }
32
33 }
34
35 </script>
36
37
38
39 <style scoped>
40
41 h1 {
42
43 color: red;
44
45 }
46
47 </style>
创建
main.js
入口文件:
1 // 导入 Vue 组件
2
3 import Vue from 'vue'
4
5
6
7 // 导入 App组件
8
9 import App from './components/App.vue'
10
11
12
13 // 创建一个 Vue 实例,使用 render 函数,渲染指定的组件
14
15 var vm = new Vue({
16
17 el: '#app',
18
19 render: c => c(App)
20
21 });
2. 在使用 webpack 构建的 Vue 项目中使用模板对象?
在
webpack.config.js
中添加resolve
属性:
1resolve: {
2 alias: {
3 'vue$': 'vue/dist/vue.esm.js'
4 }
5 }
3. ES6中语法使用总结
使用
export default
和export
导出模块中的成员; 对应ES5中的module.exports
和export
使用
import ** from **
和import '路径'
还有import {a, b} from '模块标识'
导入其他模块使用箭头函数:
(a, b)=> { return a-b; }
4. 在 vue 组件页面中,集成 vue-router 路由模块
vue-router官网
导入路由模块:
1import VueRouter from 'vue-router'
安装路由模块:
1Vue.use(VueRouter);
导入需要展示的组件:
1import login from './components/account/login.vue'
2
3import register from './components/account/register.vue'
创建路由对象:
1var router = new VueRouter({
2
3 routes: [
4
5 { path: '/', redirect: '/login' },
6
7 { path: '/login', component: login },
8
9 { path: '/register', component: register }
10
11 ]
12
13});
将路由对象,挂载到 Vue 实例上:
1var vm = new Vue({
2
3 el: '#app',
4
5 // render: c => { return c(App) }
6
7 render(c) {
8
9 return c(App);
10
11 },
12
13 router // 将路由对象,挂载到 Vue 实例上
14
15});
改造App.vue组件,在 template 中,添加
router-link
和router-view
:
1 <router-link to="/login">登录</router-link>
2
3 <router-link to="/register">注册</router-link>
4
5
6
7 <router-view></router-view>
5. 使用 饿了么的 MintUI 组件
Github 仓储地址
Mint-UI官方文档
导入所有 MintUI 组件:
1import MintUI from 'mint-ui'
导入样式表:
1import 'mint-ui/lib/style.css'
在 vue 中使用 MintUI:
1Vue.use(MintUI)
使用的例子:
1<mt-button type="primary" size="large">primary</mt-button>
6. 使用 MUI 组件
官网首页
文档地址
导入 MUI 的样式表:
1import '../lib/mui/css/mui.min.css'
在
webpack.config.js
中添加新的loader规则:
1{ test: /\.(png|jpg|gif|ttf)$/, use: 'url-loader' }
根据官方提供的文档和example,尝试使用相关的组件
7. 将项目源码托管到 oschina 中
点击头像 -> 修改资料 -> SSH公钥 如何生成SSH公钥
创建自己的空仓储,使用
git config --global user.name "用户名"
和git config --global user.email ***@**.com
来全局配置提交时用户的名称和邮箱使用
git init
在本地初始化项目使用
touch README.md
和touch .gitignore
来创建项目的说明文件和忽略文件;使用
git add .
将所有文件托管到 git 中使用
git commit -m "init project"
将项目进行本地提交使用
git remote add origin 仓储地址
将本地项目和远程仓储连接,并使用origin最为远程仓储的别名使用
git push -u origin master
将本地代码 push 到仓储中
8. App.vue 组件的基本设置
头部的固定导航栏使用
Mint-UI
的Header
组件;底部的页签使用
mui
的tabbar
;购物车的图标,使用
icons-extra
中的mui-icon-extra mui-icon-extra-cart
,同时,应该把其依赖的字体图标文件mui-icons-extra.ttf
,复制到fonts
目录下!将底部的页签,改造成
router-link
来实现单页面的切换;Tab Bar 路由激活时候设置高亮的两种方式:
全局设置样式如下:
1 .router-link-active{
color:#007aff !important;
2}
或者在
new VueRouter
的时候,通过linkActiveClass
来指定高亮的类:1 // 创建路由对象
2
3var router = new VueRouter({
4
5 routes: [{ path: '/', redirect: '/home' }
6],
7
8 linkActiveClass: 'mui-active'
9
10});
9. 实现 tabbar 页签不同组件页面的切换
将 tabbar 改造成
router-link
形式,并指定每个连接的to
属性;在入口文件中导入需要展示的组件,并创建路由对象:
1 // 导入需要展示的组件
2
3 import Home from './components/home/home.vue'
4
5 import Member from './components/member/member.vue'
6
7 import Shopcar from './components/shopcar/shopcar.vue'
8
9 import Search from './components/search/search.vue'
10
11
12
13 // 创建路由对象
14
15 var router = new VueRouter({
16
17 routes: [
18
19 { path: '/', redirect: '/home' },
20
21 { path: '/home', component: Home },
22
23 { path: '/member', component: Member },
24
25 { path: '/shopcar', component: Shopcar },
26
27 { path: '/search', component: Search }
28
29 ],
30
31 linkActiveClass: 'mui-active'
32
33 });
10. 使用 mt-swipe 轮播图组件
假数据:
1lunbo: [
2
3 'http://www.itcast.cn/images/slidead/BEIJING/2017440109442800.jpg',
4
5 'http://www.itcast.cn/images/slidead/BEIJING/2017511009514700.jpg',
6
7 'http://www.itcast.cn/images/slidead/BEIJING/2017421414422600.jpg'
8
9 ]
引入轮播图组件:
1<!-- Mint-UI 轮播图组件 -->
2
3 <div class="home-swipe">
4
5 <mt-swipe :auto="4000">
6
7 <mt-swipe-item v-for="(item, i) in lunbo" :key="i">
8
9 <img :src="item" alt="">
10
11 </mt-swipe-item>
12
13 </mt-swipe>
14
15 </div>
16
17 </div>
11. 在 .vue 组件中使用 vue-resource 获取数据
运行
cnpm i vue-resource -S
安装模块导入 vue-resource 组件
1import VueResource from 'vue-resource'
在vue中使用 vue-resource 组件
1Vue.use(VueResource);
往期精选
不甘于「本该如此」,「多选参数 」值得关注