vue 学习笔记第无弹

vue 学习笔记第无弹_JAVA

1. 在 webpack 中配置 .vue 组件页面的解析

  1. 运行cnpm i vue -S将 vue 安装为运行依赖;

  2. 运行cnpm i vue-loader vue-template-compiler -D将解析转换 vue 的包安装为开发依赖;

  3. 运行cnpm i style-loader css-loader -D将解析转换 CSS 的包安装为开发依赖,因为 .vue 文件中会写 CSS 样式;

  4. 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  }
  1. 创建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>
  1. 创建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      renderc => c(App)
20
21    });

2. 在使用 webpack 构建的 Vue 项目中使用模板对象?

  1. webpack.config.js中添加resolve属性:

1resolve: {
2    alias: {
3      'vue$''vue/dist/vue.esm.js'
4    }
5  }

3. ES6中语法使用总结

  1. 使用 export default 和 export 导出模块中的成员; 对应ES5中的 module.exports 和 export

  2. 使用 import ** from ** 和 import '路径' 还有 import {a, b} from '模块标识' 导入其他模块

  3. 使用箭头函数:(a, b)=&gt; { return a-b; }

4. 在 vue 组件页面中,集成 vue-router 路由模块

vue-router官网

  1. 导入路由模块:

1import VueRouter from 'vue-router'
  1. 安装路由模块:

1Vue.use(VueRouter);
  1. 导入需要展示的组件:

1import login from './components/account/login.vue'
2
3import register from './components/account/register.vue'
  1. 创建路由对象:

 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});
  1. 将路由对象,挂载到 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});
  1. 改造App.vue组件,在 template 中,添加router-linkrouter-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官方文档

  1. 导入所有 MintUI 组件:

1import MintUI from 'mint-ui'
  1. 导入样式表:

1import 'mint-ui/lib/style.css'
  1. 在 vue 中使用 MintUI:

1Vue.use(MintUI)
  1. 使用的例子:

1<mt-button type="primary" size="large">primary</mt-button>

6. 使用 MUI 组件

官网首页

文档地址

  1. 导入 MUI 的样式表:

1import '../lib/mui/css/mui.min.css'
  1. webpack.config.js中添加新的loader规则:

1test: /\.(png|jpg|gif|ttf)$/, use: 'url-loader' }
  1. 根据官方提供的文档和example,尝试使用相关的组件

7. 将项目源码托管到 oschina 中

  1. 点击头像 -> 修改资料 -> SSH公钥 如何生成SSH公钥

  2. 创建自己的空仓储,使用 git config --global user.name "用户名" 和 git config --global user.email ***@**.com 来全局配置提交时用户的名称和邮箱

  3. 使用 git init 在本地初始化项目

  4. 使用 touch README.md 和 touch .gitignore 来创建项目的说明文件和忽略文件;

  5. 使用 git add . 将所有文件托管到 git 中

  6. 使用 git commit -m "init project" 将项目进行本地提交

  7. 使用 git remote add origin 仓储地址将本地项目和远程仓储连接,并使用origin最为远程仓储的别名

  8. 使用 git push -u origin master 将本地代码 push 到仓储中

8. App.vue 组件的基本设置

  1. 头部的固定导航栏使用 Mint-UI 的 Header 组件;

  2. 底部的页签使用 mui 的 tabbar;

  3. 购物车的图标,使用 icons-extra 中的 mui-icon-extra mui-icon-extra-cart,同时,应该把其依赖的字体图标文件 mui-icons-extra.ttf,复制到 fonts 目录下!

  4. 将底部的页签,改造成 router-link 来实现单页面的切换;

  5. 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 页签不同组件页面的切换

  1. 将 tabbar 改造成 router-link 形式,并指定每个连接的 to属性;

  2. 在入口文件中导入需要展示的组件,并创建路由对象:

 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 轮播图组件

  1. 假数据:

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. 引入轮播图组件:

 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 获取数据

  1. 运行cnpm i vue-resource -S安装模块

  2. 导入 vue-resource 组件

1import VueResource from 'vue-resource'
  1. 在vue中使用 vue-resource 组件

1Vue.use(VueResource);




 往期精选


1. VUE 学习第一天

2. vue 学习笔记第二弹

3. vue 学习笔记第三弹

4. vue 学习笔记第四弹 -  Webpack


不甘于「本该如此」,多选参数 值得关注