vue-cli实例:

用WebStrom查看已构建的项目:

一、构建一个简单的Vue导航栏菜单实例:

1、新建组件文件夹(pages)及文件(index、userCenter、userInfo): 

index.vue代码:

<template>
  <div>
    <p>这是首页</p>
  </div>
</template>

<script>
export  default {

}
</script>

<style>
p{
  background: #ffe87c;
}
</style>

userCenter.vue代码:

<template>
  <div>
    <p>这是个人中心</p>
    <router-link to="/userCenter/userInfo">用户信息</router-link>
    <router-view></router-view>
  </div>
</template>

<script>
export default {

}
</script>

<style>
  p{
    background: #d6e9c6;
  }
</style>

userInfo.vue代码:

<template>
  <div>
    <p>My name is Samve Duan!</p>
  </div>
</template>

<script>
export default {}
</script>

<style>
  p{
    background: #77FFFF;
  }
</style>

2.在路由配置文件中,导入新建的组件。(index.js我们不用了)

import Vue from 'vue'
import Router from 'vue-router'
import Index from '../pages/index'
import UserCenter from '../pages/userCenter'
import UserInfo from '../pages/userInfo'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/index',
      component: Index
    },
    {
      path: '/userCenter',
      component: UserCenter,
      children: [
        {
          path: 'userInfo',
          component: UserInfo
        }
      ]
    }
  ]
});

3.在项目入口App.vue中建立导航栏,代码如下:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <p>这可以看做是导航栏</p>
    <router-link to="/index">首页</router-link>
    <router-link to="/userCenter">个人中心</router-link>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'App',
  components: {

  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

4.修改main.js 

import router from './router'

改为:

import router from './router/router.js'

main.js代码:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router/router.js'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

二、点击添加用户:

User.vue:

<template>
  <div>
    <h1>Users</h1>
    <ul>
      <li v-for="user in users"><input v-model="user.contacted" type="checkbox"/><span :class="{deleteLine: user.contacted}">{{user.name}}:{{user.email}}</span> <button @click="deleteUser(user)">x</button></li>
    </ul>
    <form @submit="addUser">
    name: <input v-model="user.name"/>
    email: <input v-model="user.email"/>
    <p><input type="submit" value="添加用户"/></p>
    </form>
  </div>
</template>

<script>
export default {
  name: 'users',
  data: function(){
    return {
      user: {},
      users: [
        {
          name: 'zhoujielun',
          email: 'zhoujielun@gmail.com',
          contacted: false
        },
        {
          name: 'zhouhuajian',
          email: 'zhouhuajian@gmail.com',
          contacted: false
        },
        {
          name: 'youhongming',
          email: 'youhongming@gmail.com',
          contacted: false
        }
      ]
    }
  },
  methods: {
    addUser: function(e){
      this.users.push({
        name: this.user.name,
        email: this.user.email,
        contacted: false
      });
      this.user = {};
      e.preventDefault();
    },
    deleteUser: function(index){
      this.users.splice(this.users.indexOf(index), 1)
    }
  }
}
</script>

<style scoped="">
ul{
  list-style-type: none;
}
ul li{
  list-style-type: none; line-height: 22px;
}
.deleteLine{
  text-decoration: line-through;
}
</style>

app.vue:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <users></users>
  </div>
</template>

<script>
import Users from './components/Users.vue'
export default {
  name: 'App',
  components: {
    Users
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

三、使用vue-resource从第三方读取数据:

打开项目所在文件夹,安装vue-resource:

npm install vue-resource --save

Users.vue:

<template>
  <div>
    <h1>Users</h1>
    <ul>
      <li v-for="user in users"><input v-model="user.contacted" type="checkbox"/>{{user.contacted}}<span :class="{deleteLine: user.contacted}">{{user.name}}:{{user.email}}</span> <button @click="deleteUser(user)">x</button></li>
    </ul>
    <form @submit="addUser">
    name: <input v-model="user.name"/>
    email: <input v-model="user.email"/>
    <p><input type="submit" value="添加用户"/></p>
    </form>
  </div>
</template>

<script>
export default {
  name: 'users',
  data: function(){
    return {
      user: {},
      users: [
        {
          name: 'zhoujielun',
          email: 'zhoujielun@gmail.com',
          contacted: false
        },
        {
          name: 'zhouhuajian',
          email: 'zhouhuajian@gmail.com',
          contacted: false
        },
        {
          name: 'youhongming',
          email: 'youhongming@gmail.com',
          contacted: false
        }
      ]
    }
  },
  methods: {
    addUser: function(e){
      this.users.push({
        name: this.user.name,
        email: this.user.email,
        contacted: false
      });
      this.user = {};
      e.preventDefault();
    },
    deleteUser: function(index){
      this.users.splice(this.users.indexOf(index), 1)
    }
  },
  created: function(){
    this.$http.get('http://jsonplaceholder.typicode.com/users').then(function(response){
      this.users = response.data;
    })
  }
}
</script>

<style scoped="">
ul{
  list-style-type: none;
}
ul li{
  list-style-type: none; line-height: 22px;
}
.deleteLine{
  text-decoration: line-through;
}
</style>

app.vue:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <users></users>
  </div>
</template>

<script>
import Users from './components/Users.vue'
export default {
  name: 'App',
  components: {
    Users
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

main.js:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import VueResource from 'vue-resource'//全局安装vue-resource

Vue.use(VueResource)

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>'
})

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值