1. 介绍
前面入门了nodejs服务端,这里一起来学习最近很火的前端框架vue.js.官网目前版本为vue2.0.
2. 环境搭建
使用vue-cli脚手架工具构建
全局安装vue-cli
1
| npm install -g vue-cli -d (最后一个参数-d是在命令行显示安装进度)
|
查看安装成功与否 vue -V(大写)
使用vue-cli初始化项目
1
| vue init webpack vue-hello-world
|
初始化项目的时候,出现在填写项目描述的时候无法继续往下走。查明原因:node版本问题(v-8.1.1)。解决方法:将node版本降到v-8.0.0或者v-6.10.2
创建项目过程命令介绍如下图:

项目创建完毕后目录结构如下图:

3. 安装依赖
1 2
| cd vue-hello-world npm install
|
如果在安装依赖过程中下载速度慢,可以切换到淘宝镜像
1 2 3
| a. 通过config命令 npm config set registry https: npm info underscore (如果上面配置正确这个命令会有字符串response)
|
1
| b. npm install -g cnpm --registry=https:
|
4. 运行项目
如果浏览器打开后,页面没有加载出来,有可能是8080端口被其他项目占用。需要修改配置文件 config->index.js
6. 打包
另外将( config>index.js-> assetsPublicPath) build 的路径前缀修改为 ‘ ./ ‘(原本为 ‘ / ‘),是因为打包之后,外部引入 js 和 css 文件时,如果路径以 ‘ / ‘ 开头,在本地是无法找到对应文件的(服务器上没问题)。所以如果需要在本地打开打包后的文件,就得修改文件路径。
流程说明
首先会打开首页 index.html文件
使用webpack打包之后默认加载main.js。并且会嵌入到index.html文件中
上面提到的就是一个项目的从环境搭建到打包发布的过程,接下来进行开发。
直接上代码:main.js
1 2 3 4 5 6 7
| import Vue from 'vue' import App from './App' import router from './router'
import VueResource from 'vue-resource' Vue.use(VueResource)
|
实例化vue对象
1 2 3 4 5 6 7 8 9 10 11
| new Vue({ el: '#app', router, render: h => h(App)
})
|
App.vue文件是项目的组件入口,所有的开发都在这里进行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <template> <div id="app"> <!-- <hello></hello> --> <div class="nav"> <!-- 使用 router-link 组件来导航. --> <!-- 通过传入 `to` 属性指定链接. --> <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 --> <ul> <li><router-link to="/home">Home</router-link></li> <li><router-link to="/about">About</router-link></li> <li><router-link to="/hello">hello</router-link></li> </ul> </div> <div class="main"> <!-- 路由匹配到的组件将渲染在这里 -->
<router-view></router-view> </div> </div> </template>
<script> //import Hello from './components/Hello'
export default { name: 'app', components: { //Hello } } </script>
<style> body{ background-color: #f8f8ff; font-family: 'Avenir', Helvetica, Arial, sans-serif; color: #2c3e50; }
.nav{ position: fixed; width: 108px; left: 40px; } .nav ul{ list-style: none; margin: 0; padding: 0; } .nav ul li{ width: 108px; height: 48px; line-height: 48px; border:1px solid #dadada; text-align: center; } .nav ul li a{ text-decoration: none; }
.main{ height: 400px; margin-left: 180px; margin-right: 25px; }
</style>
|
项目中使用到了路由,所以要到router/index.js文件中进行路由配置和映射,并通过export暴露router到main.js中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter)
const router = new VueRouter({ routes:[ { path: '/home', component: require('../components/Home.vue') }, { path: '/about', component: require('../components/About.vue') }, { path: '/hello', component: require('../components/Hello.vue') }, { path:'*',redirect:'/home' } ] })
export default router;
|
点击about和hello导航会映射到对应的组件,然后渲染到App.vue里面的
前面在main.js文件中移入了网络请求模块vue-resource,接着创建Home.vue组件并在created函数中去请求网络。
Home.vue代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| <template> <div class="home"> <h1>{{ msg }}</h1> <ul> <li v-for="article in articles"> <div class="m-img inl-block"><img v-bind:src="article.images.small"/></div> <div class="m-content inl-block"> <div>{{article.title}}</div> <div>年份:{{article.year}}</div> <div>类型:{{article.subtype}}</div> </div> </li> </ul> </div> </template> <script>
export default { name: 'home', data () { return { msg: '电影列表', articles:[] } }, created:function(){ this.$http.jsonp('https://api.douban.com/v2/movie/top250?count=10', {}, { headers: { }, emulateJSON: true }).then(function(response) { console.log(response); this.articles = response.data.subjects
}, function(response) { console.log(response) }); } } </script>
<!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> ul{ list-style: none; margin: 0; padding: 0; } ul li{ border-bottom: 1px solid #999; padding: 10px 0; } .inl-block{ display: inline-block; }
.m-img{ } .m-content{ margin-left: 20px; } </style>
|
最后运行整个项目
效果图:

源码地址