一、组件嵌套

1. 新建一个组件Users.vue

  1. <template>
  2. <div class="users">
  3. <ul>
  4. <li v-for="user in users">
  5. {{user}}
  6. </li>
  7. </ul>
  8.  
  9. </div>
  10. </template>
  11.  
  12. <script>
  13. export default {
  14. name: 'users',
  15. data () {
  16. return {
  17. users:["henry", "bucky", "emily"]
  18. }
  19. }
  20. }
  21. </script>
  22.  
  23. <!-- Add "scoped" attribute to limit CSS to this component only -->
  24. <style scoped>
  25.  
  26. </style>

2. 全局注册组件

2.1. 在main.js里面引入组件Users.vue

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue' //导入vue
  4. import App from './App' //导入根组件App.vue
  5. import Users from './components/Users'//导入组件Users.vue
  6.  
  7. Vue.config.productionTip = false
  8.  
  9. //全局注册组件Users.vue
  10. Vue.component("users",Users);
  11.  
  12. /* eslint-disable no-new */
  13. new Vue({ //实例化一个vue对象
  14. el: '#app', //index.html的根元素app
  15. components: { App },//注册根组件App.vue才能使用
  16. template: '<App/>'//VUE模板使用,可以是组件、html标签等
  17. })

2.2. 在根组件App.vue里面使用组件users

  1. // 1. 模板:html结构 有且只有一个根标签
  2. <template>
  3. <div id="app">
  4. <HelloWorld/> <!--使用组件HelloWorld -->
  5. <users></users><!--使用组件users -->
  6. </div>
  7. </template>
  8.  
  9. //2. 行为:处理逻辑
  10. <script>
  11. //导入组件HelloWorld.vue
  12. import HelloWorld from './components/HelloWorld'
  13.  
  14. export default {
  15. name: 'App',//组件App.vue的名字
  16. components: {
  17. HelloWorld, //注册组件HelloWorld才能使用
  18. }
  19. }
  20. </script>
  21.  
  22. //3. 样式:解决样式
  23. <style>
  24. #app {
  25. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  26. -webkit-font-smoothing: antialiased;
  27. -moz-osx-font-smoothing: grayscale;
  28. text-align: center;
  29. color: #2c3e50;
  30. margin-top: 60px;
  31. }
  32. </style>

3、局部注册组件

3.1. 删除main.js里面注册的全局组件users

3.2. 在根组件app.vue里面局部注册和使用组件

  1. // 1. 模板:html结构 有且只有一个根标签
  2. <template>
  3. <div id="app">
  4. <users></users><!--使用组件users -->
  5. </div>
  6. </template>
  7.  
  8. //2. 行为:处理逻辑
  9. <script>
  10. //局部注册组件Users.vue
  11. import Users from './components/Users'//导入组件Users.vue
  12.  
  13. export default {
  14. name: 'App',//组件App.vue的名字
  15. components: {
  16. "users" : Users //注册组件Users.vue才能使用
  17. }
  18. }
  19. </script>
  20.  
  21. //3. 样式:解决样式
  22. <style>
  23. #app {
  24. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  25. -webkit-font-smoothing: antialiased;
  26. -moz-osx-font-smoothing: grayscale;
  27. text-align: center;
  28. color: #2c3e50;
  29. margin-top: 60px;
  30. }
  31. </style>

4、页面呈现效果

二、组件CSS的作用域

1. 分别在在组件App.vue和Users.vue里面定义一个h1的样式

App.vue

  1. // 1. 模板:html结构 有且只有一个根标签
  2. <template>
  3. <div id="app">
  4. <h1>App</h1>
  5. <users></users><!--使用组件users -->
  6. </div>
  7. </template>
  8.  
  9. //2. 行为:处理逻辑
  10. <script>
  11. //局部注册组件Users.vue
  12. import Users from './components/Users'//导入组件Users.vue
  13.  
  14. export default {
  15. name: 'App',//组件App.vue的名字
  16. components: {
  17. "users" : Users //注册组件Users.vue才能使用
  18. }
  19. }
  20. </script>
  21.  
  22. //3. 样式:解决样式
  23. <style>
  24. h1
  25. {
  26. color:purple;
  27. }
  28. </style>

Users.vue

  1. <template>
  2. <div class="users">
  3. <h1>users</h1>
  4.  
  5. </div>
  6. </template>
  7.  
  8. <script>
  9. export default {
  10. name: 'users',
  11. data () {
  12. return {
  13. users:["henry", "bucky", "emily"]
  14. }
  15. }
  16. }
  17. </script>
  18.  
  19. <!-- Add "scoped" attribute to limit CSS to this component only -->
  20. <style >
  21. h1
  22. {
  23. color: green;
  24. }
  25. </style>

查看页面效果可以发现App.vue里面的h1的样式被Users.vue里面的样式给覆盖了

那么我们要他们各自使用自己的h1的样式怎么办呢?

我分别给他们加上scoped就可以了

App.vue的样式:

  1. //3. 样式:解决样式
  2. <style scoped>
  3. h1
  4. {
  5. color:purple;
  6. }
  7. </style>

Users.vue的样式:

  1. <style scoped>
  2. h1
  3. {
  4. color: green;
  5. }
  6. </style>

页面效果:

三、属性传值props(父组件把数据传递给子组件)

把父组件App.vue的数据传递给子组件Users.vue使用

App.vue

  1. // 1. 模板:html结构 有且只有一个根标签
  2. <template>
  3. <div id="app">
  4. <h1>App</h1>
  5. <users v-bind:users="people"></users><!--使用组件users的时候把父组件的数据people传递给它 -->
  6. </div>
  7. </template>
  8.  
  9. //2. 行为:处理逻辑
  10. <script>
  11. //局部注册组件Users.vue
  12. import Users from './components/Users'//导入组件Users.vue
  13.  
  14. export default {
  15. name: 'App',//组件App.vue的名字
  16. components: {
  17. "users" : Users //注册组件Users.vue才能使用
  18. },
  19. data () {
  20. return {
  21. people:["henry", "bucky", "emily"]
  22. }
  23. }
  24. }
  25. </script>
  26.  
  27. //3. 样式:解决样式
  28. <style scoped>
  29. h1
  30. {
  31. color:purple;
  32. }
  33. </style>

Users.vue

  1. <template>
  2. <div class="users">
  3. <h1>users</h1>
  4. <ul>
  5. <!-- 遍历父组件传递过来的users -->
  6. <li v-for="user in users">
  7. {{user}}
  8. </li>
  9. </ul>
  10.  
  11. </div>
  12. </template>
  13.  
  14. <script>
  15. export default {
  16. name: 'users',
  17. // props:["users"],//获取父组件传递过来的users
  18. props:{
  19. users:{
  20. type:Array,
  21. required:true
  22. }
  23. },
  24. data () {
  25. return {
  26.  
  27. }
  28. }
  29. }
  30. </script>
  31.  
  32. <!-- Add "scoped" attribute to limit CSS to this component only -->
  33. <style scoped>
  34. h1
  35. {
  36. color: green;
  37. }
  38. </style>

页面效果:

四、事件传值(子组件把数据传递给父组件)

在子组件Users.vue的方法changeTitle里面注册一个事件titleChanged并传递一个参数值:"子向父传值"

  1. <template>
  2. <div class="users">
  3. <h1 v-on:click="changeTitle">users</h1>
  4. <ul >
  5. <!-- 遍历父组件传递过来的users -->
  6. <li v-for="user in users">
  7. {{user}}
  8. </li>
  9. </ul>
  10.  
  11. </div>
  12. </template>
  13.  
  14. <!--
  15.  
  16. 传值: string、number、boolean
  17. 传引用:Array、Object
  18.  
  19. -->
  20. <script>
  21. export default {
  22. name: 'users',
  23. // props:["users"],//获取父组件传递过来的users
  24. props:{
  25. users:{
  26. type:Array,
  27. required:true
  28. }
  29. },
  30. data () {
  31. return {
  32.  
  33. }
  34. },
  35. methods : {
  36. changeTitle : function(){
  37. //注册一个事件titleChanged
  38. this.$emit("titleChanged","子向父传值");
  39. }
  40.  
  41. }
  42. }
  43. </script>
  44.  
  45. <!-- Add "scoped" attribute to limit CSS to this component only -->
  46. <style scoped>
  47. h1
  48. {
  49. width: 1000px;
  50. height: 100px;
  51. background-color: aqua;
  52. color: green;
  53. }
  54. </style>

在父组件里面实现事件titleChanged

  1. // 1. 模板:html结构 有且只有一个根标签
  2. <template>
  3. <div id="app">
  4. <p v-on:titleChanged="updateTitle($event)">App</p>
  5. {{title}}
  6. <users v-bind:users="people"></users><!--使用组件users的时候把父组件的数据people传递给它 -->
  7. </div>
  8. </template>
  9.  
  10. //2. 行为:处理逻辑
  11. <script>
  12. //局部注册组件Users.vue
  13. import Users from './components/Users'//导入组件Users.vue
  14.  
  15. export default {
  16. name: 'App',//组件App.vue的名字
  17. components: {
  18. "users" : Users //注册组件Users.vue才能使用
  19. },
  20. data () {
  21. return {
  22. people:["henry", "bucky", "emily"],
  23. title : "父组件的"
  24. }
  25. },
  26. methods : {
  27. updateTitle (msg){
  28. this.title=msg;
  29. }
  30. }
  31. }
  32. </script>
  33.  
  34. //3. 样式:解决样式
  35. <style scoped>
  36. h1
  37. {
  38. color:purple;
  39. }
  40. </style>

五、传值和传引用

传值: 传递的是string、number、boolean,多处地方使用,改变一处的值,其他的地方的值不会受影响
传引用:传递的是Array、Object,多处地方使用,改变一处的值,其他的地方的值会受影响
传值和传引用类似于C语言中的传值和传址

六、生命周期(钩子函数)

1. 生命周期的作用

1. 在开发过程中定位问题
2. 解决需求问题,即什么需求应该写在生命周期的哪个阶段

2. 生命周期的流程图:

流程图里面的钩子函数说明:

beforeCreate : 实例化vue对象之前做的一些动作,这里有一些事件的初始化,在这里可以做页面的loading

create:在这里vue对象已经实例化完毕,事件也初始化完毕,在这里可以做网络接口数据的请求、结束beforeCreate里面的loading

    create之后检查有没有el元素,如果有就继续看有没有template,如果这两个都没有,生命周期就结束了;如果有template就里面是一个组件还是一个html元素

beforeMount:在这个方法之后进行虚拟dom的渲染,注意:此时只是进行虚拟dom的渲染,页面还没显示出来

mounted :执行这个方法之后页面就显示出来了

beforeUpdate:页面进行更新之前要做的事

updated:这个方法执行完成之后页面修改的数据已改变

beforeDestroy:生命周期结束之前

destroyed:生命周期结束

3. 生命周期钩子函数的示例

Users.vue

  1. <template>
  2. <div class="users">
  3. <h1 v-on:click="changeTitle">users</h1>
  4. <ul >
  5. <!-- 遍历父组件传递过来的users -->
  6. <li v-for="user in users">
  7. {{user}}
  8. </li>
  9. </ul>
  10.  
  11. </div>
  12. </template>
  13.  
  14. <!--
  15.  
  16. 传值: string、number、boolean
  17. 传引用:Array、Object
  18.  
  19. -->
  20. <script>
  21. export default {
  22. name: 'users',
  23. // props:["users"],//获取父组件传递过来的users
  24. props:{
  25. users:{
  26. type:Array,
  27. required:true
  28. }
  29. },
  30. data () {
  31. return {
  32.  
  33. }
  34. },
  35. methods : {
  36. changeTitle : function(){
  37. //注册一个事件titleChanged
  38. this.$emit("titleChanged","子向父传值");
  39. }
  40.  
  41. },
  42. beforeCreate:function(){
  43. alert("组件实例化之前执行的函数");
  44. },
  45. created:function(){
  46. alert("组件实例化完毕,但页面还未显示");
  47. },
  48. beforeMount:function(){
  49. alert("组件挂载前,页面仍未展示,但虚拟Dom已经配置");
  50. },
  51. mounted:function(){
  52. alert("组件挂载后,此方法执行后,页面显示");
  53. },
  54. beforeUpdate:function(){
  55. alert("组件更新前,页面仍未更新,但虚拟Dom已经配置");
  56. },
  57. updated:function(){
  58. alert("组件更新,此方法执行后,页面显示");
  59. },
  60. beforeDestory:function(){
  61. alert("组件销毁前");
  62. },
  63. destoryed:function(){
  64. alert("组件销毁");
  65. }
  66. }
  67. </script>
  68.  
  69. <!-- Add "scoped" attribute to limit CSS to this component only -->
  70. <style scoped>
  71. h1
  72. {
  73. width: 1000px;
  74. height: 100px;
  75. background-color: aqua;
  76. color: green;
  77. }
  78. </style>

七、路由和http

路由

路由和a标签一样实现页面的跳转,和a标签最大的不同是路由的性能更好,原因是a标签点击多次就跳转多次性能很低,而路由的话不管你点击多少次都只跳转一次

要使用路由先安装路由:
安装路由之前要先停止项目:ctrl+c
安装路由的命令:npm install vue-router --save-dev
安装完路由之后启动项目:npm run dev

路由使用示例:

main.js

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue' //导入vue
  4. import VueRouter from 'vue-router' //引入路由
  5. import App from './App' //导入根组件App.vue
  6. import Users from './components/Users'//导入组件Users.vue
  7. import Home from './components/Home'//导入组件Users.vue
  8.  
  9. Vue.config.productionTip = false
  10. //引入路由后使用路由
  11. Vue.use(VueRouter)
  12. //配置路由
  13. const router = new VueRouter(
  14. {
  15. routes : [
  16. //配置路由跳转到Home这个组件
  17. {path:"/",component:Home},
  18. //配置路由跳转到Users.vue这个组件
  19. {path:"/Users",component:Users}
  20. ],
  21. //去掉地址栏的"/#/"
  22. mode : "history"
  23. }
  24. )
  25.  
  26. /* eslint-disable no-new */
  27. new Vue({ //实例化一个vue对象
  28. router,//使用路由
  29. el: '#app', //index.html的根元素app
  30. components: { App },//注册根组件App.vue才能使用
  31. template: '<App/>'//VUE模板使用,可以是组件、html标签等
  32. })

新建一个Home.vue

  1. <template>
  2. <div class="home">
  3. <h1 >home</h1>
  4. <ul >
  5. <!-- 遍历父组件传递过来的users -->
  6. <li v-for="user in users">
  7. {{user}}
  8. </li>
  9. </ul>
  10.  
  11. </div>
  12. </template>
  13.  
  14. <!--
  15.  
  16. 传值: string、number、boolean
  17. 传引用:Array、Object
  18.  
  19. -->
  20. <script>
  21. export default {
  22. name: 'home',
  23. data () {
  24. return {
  25. users:["henry", "bucky", "emily"]
  26. }
  27. }
  28. }
  29. </script>
  30.  
  31. <!-- Add "scoped" attribute to limit CSS to this component only -->
  32. <style scoped>
  33. h1
  34. {
  35. width: 1000px;
  36. height: 100px;
  37. background-color: aqua;
  38. color: green;
  39. }
  40. </style>

Users.vue

  1. <template>
  2. <div class="users">
  3. <h1 >users</h1>
  4.  
  5. </div>
  6. </template>
  7.  
  8. <!--
  9.  
  10. 传值: string、number、boolean
  11. 传引用:Array、Object
  12.  
  13. -->
  14. <script>
  15. export default {
  16. name: 'users',
  17. data () {
  18. return {
  19.  
  20. }
  21. }
  22. }
  23. </script>
  24.  
  25. <!-- Add "scoped" attribute to limit CSS to this component only -->
  26. <style scoped>
  27. h1
  28. {
  29. width: 1000px;
  30. height: 100px;
  31. background-color: aqua;
  32. color: green;
  33. }
  34. </style>

在根组件App.vue里面使用路由

  1. // 1. 模板:html结构 有且只有一个根标签
  2. <template>
  3. <div id="app">
  4. <!-- 使用路由实现跳转 注意:to里面写的是路由的名字 -->
  5. <ul>
  6. <li><router-link to="/">home</router-link></li>
  7. <li><router-link to="/Users">Users</router-link></li>
  8. </ul>
  9. <!-- 进入首页默认加载的路由 -->
  10. <router-view></router-view>
  11. </div>
  12. </template>
  13.  
  14. //2. 行为:处理逻辑
  15. <script>
  16.  
  17. export default {
  18. name: 'App',//组件App.vue的名字
  19. data () {
  20. return {
  21.  
  22. }
  23. }
  24. }
  25. </script>
  26.  
  27. //3. 样式:解决样式
  28. <style scoped>
  29. h1
  30. {
  31. color:purple;
  32. }
  33. </style>

页面效果:

首页

点击Users跳转到Users组件

点击home跳转到Home组件

使用http

要使用http要先安装vue-resource
安装路由之前要先停止项目:ctrl+c
安装路由的命令:npm install vue-resource --save-dev
安装完路由之后启动项目:npm run dev

http使用示例:

在main.js里面引入vue-resource就可以在任何组件中使用了http了

  1. // The Vue build version to load with the `import` command
  2. // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
  3. import Vue from 'vue' //导入vue
  4. import VueRouter from 'vue-router' //引入路由
  5. import VueResource from 'vue-resource' //引入vue-resource
  6. import App from './App' //导入根组件App.vue
  7. import Users from './components/Users'//导入组件Users.vue
  8. import Home from './components/Home'//导入组件Users.vue
  9.  
  10. Vue.config.productionTip = false
  11. //引入路由后使用路由,这样就可以在任何组件中使用路由了
  12. Vue.use(VueRouter)
  13. //引入vue-resource后使用vue-resource,这样就可以在任何组件中使用http了
  14. Vue.use(VueResource)
  15. //配置路由
  16. const router = new VueRouter(
  17. {
  18. routes : [
  19. //配置路由跳转到Home这个组件
  20. {path:"/",component:Home},
  21. //配置路由跳转到Users.vue这个组件
  22. {path:"/Users",component:Users}
  23. ],
  24. //去掉地址栏的"/#/"
  25. mode : "history"
  26. }
  27. )
  28.  
  29. /* eslint-disable no-new */
  30. new Vue({ //实例化一个vue对象
  31. router,//使用路由
  32. el: '#app', //index.html的根元素app
  33. components: { App },//注册根组件App.vue才能使用
  34. template: '<App/>'//VUE模板使用,可以是组件、html标签等
  35. })

在Home.vue组件中发送http请求

  1. <template>
  2. <div class="home">
  3. <h1 >home</h1>
  4. <ul >
  5. <!-- 遍历父组件传递过来的users -->
  6. <li v-for="user in users">
  7. {{user}}
  8. </li>
  9. </ul>
  10.  
  11. </div>
  12. </template>
  13.  
  14. <!--
  15.  
  16. 传值: string、number、boolean
  17. 传引用:Array、Object
  18.  
  19. -->
  20. <script>
  21. export default {
  22. name: 'home',
  23. data () {
  24. return {
  25. users:["henry", "bucky", "emily"]
  26. }
  27. },
  28. created(){
  29. this.$http.get("http://jsonplaceholder.typicode.com/users")
  30. .then((data) => {
  31. console.log(data);
  32. // this.users = data.body;
  33. })
  34. }
  35. }
  36. </script>
  37.  
  38. <!-- Add "scoped" attribute to limit CSS to this component only -->
  39. <style scoped>
  40. h1
  41. {
  42. width: 1000px;
  43. height: 100px;
  44. background-color: aqua;
  45. color: green;
  46. }
  47. </style>

请求结果:

VUE系列二:vue基础的更多相关文章

  1. Mina 系列(二)之基础

    Mina 系列(二)之基础 Mina 使用起来多么简洁方便呀,就是不具备 Java NIO 的基础,只要了解 Mina 常用的 API,就可以灵活使用并完成应用开发. 1. Mina 概述 首先,看 ...

  2. Vue系列---理解Vue.nextTick使用及源码分析(五)

    _ 阅读目录 一. 什么是Vue.nextTick()? 二. Vue.nextTick()方法的应用场景有哪些? 2.1 更改数据后,进行节点DOM操作. 2.2 在created生命周期中进行DO ...

  3. Vue系列之 => vue组件创建

    创建方式 一 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  4. vue系列---理解Vue中的computed,watch,methods的区别及源码实现(六)

    _ 阅读目录 一. 理解Vue中的computed用法 二:computed 和 methods的区别? 三:Vue中的watch的用法 四:computed的基本原理及源码实现 回到顶部 一. 理解 ...

  5. Vue系列:Vue Router 路由梳理

    Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...

  6. Vue系列之 => webpack基础使用

    webpack安装方式 1,运行 npm i webpack -g 全局安装. 2,在项目根目录中运行 npm i webpack --save-dev 安装到项目依赖中 项目目录 进入src运行, ...

  7. [js高手之路] vue系列教程 - vue的事件绑定与方法(2)

    一.在vue中,绑定事件,用v-on:事件类型, 如绑定一个点击事件, 我们可以这样子做 window.onload = function () { var c = new Vue({ el : 'b ...

  8. [js高手之路] vue系列教程 - vue的基本用法与常见指令(1)

    本系列课程选用vue的版本为1.0.21, 什么是vue? vue是由尤雨溪开发的一款基于MVVM的框架,M->模型,V->视图, 也就是说模型数据改变了,视图也跟着改变, 视图内容改变, ...

  9. Vue学习(二)-Vue中组件间传值常用的几种方式

    版本说明:vue-cli:3.0 主要分为两类: 1.父子组件间的传值 2.非父子组件间的传值 1.父子组件间传值 父组件向子组件传值 第一种方式: props 父组件嵌套的子组件中,使用v-bind ...

随机推荐

  1. Java 编程中关于异常处理的 10 个最佳实践

    异常处理是Java 开发中的一个重要部分.它是关乎每个应用的一个非功能性需求,是为了处理任何错误状况,比如资源不可访问,非法输入,空输入等等.Java提供了几个异常处理特性,以try,catch 和 ...

  2. redhat 6、7配置yum源

    # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # sed -i  ' ...

  3. Improving Performance【转】

    This section provides solutions to some performance problems, and describes configuration best pract ...

  4. angularjs去掉加载时的{{}}

    添加css <style>        .ng-cloak {display: none;}    </style> 在body头文件中加上class=ng-cloak &l ...

  5. [转]table中设置tr行间距

    原文地址:https://blog.csdn.net/itmyhome1990/article/details/50475616 CSS border-collapse 属性设置表格的边框是否被合并为 ...

  6. [转]Spring MVC 教程,快速入门,深入分析

    原文地址:http://elf8848.iteye.com/blog/875830 目录 一.前言 二.spring mvc 核心类与接口 三.spring mvc 核心流程图 四.spring mv ...

  7. 几种经典的Hash算法的实现(源代码)

    来源声明: http://blog.minidx.com/2008/01/27/446.html 先保存下来,以备后面研究,现在还看不懂! 哈希算法将任意长度的二进制值映射为固定长度的较小二进制值,这 ...

  8. Android动画知识汇总

    Interpolator(插值器) Interpolatort通常在动画中使用,主要来改变动画变化率.常用的有下面几种Interpolator,下面图文解说下: AccelerateDecelerat ...

  9. REST接口调用经验

    1. 调用接口的时候对于参数或返回值的单位一定要注意啊,比如有的分数用的百分制,有的用的小数...,坑大了

  10. Office/Access 2013 扩展支持xbase/DBF 文件

    最新的Access 2013 安装后,已经不能对dbf文件进行链接读取了,Access 2010以前的版本都可以,如果以前的项目用了Access链接大量的dbf文件的话, 升级Access 2013后 ...