vue介绍
vue的特点
- MVVM
实现了交互性,数据双向绑定
- 轻量级
- Component
双向绑定
<div id="app">
{{message}}
<input type="text" name="username" v-model="message">
</div>
<script type="text/javascript">
const appVue = new Vue({
el:'#app',
data:{
message:''
}
})
</script>
效果图如下:
列表渲染
使用属性v-for
做列表渲染:
<div id="app">
<ul>
<li v-for="u in users">
{{u.username}}
</li>
</ul>
</div>
<script type="text/javascript">
const appVue = new Vue({
el:'#app',
data:{
users:[
{username:'张三'},
{username:'李四'},
{username:'王五'},
]
}
})
</script>
运行截图:
vue路由
demo:
- html
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<h1>Hello App!</h1>
<p>
<!-- 使用 router-link 组件来导航. -->
<!-- 通过传入 `to` 属性指定链接. -->
<!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>
</p>
<!-- 路由出口 -->
<!-- 路由匹配到的组件将渲染在这里 -->
<router-view></router-view>
</div>
- js非模块化方式
(function (Vue) {
// 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)
// 1. 定义 (路由) 组件。
// 可以从其他文件 import 进来
const Foo = {template: '<div>foo...</div>'}
const Bar = {template: '<div>bar....</div>'}
// 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{path: '/foo', component: Foo},
{path: '/bar', component: Bar}
]
// 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写) 相当于 routes: routes
})
// 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router
}).$mount('#app')
})(Vue)
路由重定向
希望用户输入 /
能重定向到 /#/home
中:
const routes = [
{path:'/', redirect:{path:'/foo'}},
{path: '/foo', component: Foo},
{path: '/bar', component: Bar}
]
template
由于模板HTML代码比较多,不能写在js文件中,因此需要单独写模板代码:
- 对于非模块化项目,可以将template写在
script
标签中 - 对于模块化项目,可以新建
.vue
文件
- 非模块项目:
<script id="index_tmpl" type="text/v-template">
<div>
index....tmpl
</div>
</script>
const Home = {template: document.getElementById('index_tmpl')}
const List = {template: document.getElementById('list_tmpl')}
运行项目
- 从GitHub克隆项目:
git clone https://github.com/zce/music-player.git -b vue --depth 1
`
-b : 分支,一个项目可以有多个分支

– depth : 深度,表示保存提交历史记录次数
- 安装项目的依赖
cnpm install
(使用淘宝镜像) - 运行项目
npm run serve
($ music-player )
node app start
($ music-player 运行nodejs文件)
npm run client
($ music-player 运行客户端)
- music-player
– client
– server