组件数据局部化处理

  1. 不管页面组件还是小组件,都可能会被多次复用
  2. 复用组件的原因,其实就是复用组件的 页面结构、页面样式、页面逻辑
  3. 但是页面上的数据需要区分(被复用的两个组件数据多少是有区别的),所以组件的数据要做局部化处理
  4. 借助函数可以产生局部作用域的特点,为每一次复用组件产生一个独立的作用域

语法:

  1. data () {
  2. return {
  3. // 数据们
  4. }
  5. }

子组件

  1. <template>
  2. <div class="beat" @click="count += 1">
  3. {{ count }}下
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "Beat",
  9. // 不管是页面组件还是小组件,都可能被复用,页面结构与样式都可以采用一套,但是数据一定要相互独立
  10. data () {
  11. return {
  12. count: 0
  13. }
  14. }
  15. }
  16. </script>
  17. <style scoped>
  18. .beat {
  19. width: 100px;
  20. height: 100px;
  21. background-color: orange;
  22. text-align: center;
  23. line-height: 100px;
  24. border-radius: 50%;
  25. }
  26. </style>

父组件

  1. <template>
  2. <div class="home">
  3. <Beat/>
  4. <Beat/>
  5. </div>
  6. </template>
  7. <script>
  8. import Beat from '@/components/Beat'
  9. export default {
  10. components: {
  11. Beat,
  12. }
  13. }
  14. </script>

路由逻辑跳转

  1. 很多时候,我们需要通过普通按钮的逻辑,或是直接在某些逻辑中完成页面的跳转
  2. 可以通过在逻辑中用 this.$router.push() 来完成前往目标页,两种语法如下
    • this.$router.push('路劲')
    • this.$router.push({name: '路由名'})
  3. 在做移动端项目时,没有像浏览器那样的前进后台键,页可以用 this.$router.go() 来完成前进后退,语法如下
    • 前进后退:this.$router.go(正负整数),正式代表前进,负数代表后台,数值就是步长

案例:

  1. <template>
  2. <div class="home">
  3. <Nav/>
  4. <h1>主页</h1>
  5. <button @click="goPage('/first')">前往第一页</button>
  6. |
  7. <button @click="goPage('/second')">前往第二页</button>
  8. |
  9. <button @click="goBack(-1)">后退一页</button>
  10. |
  11. <button @click="goBack(-2)">后退二页</button>
  12. |
  13. <button @click="goBack(1)">前进一页</button>
  14. </div>
  15. </template>
  16. <script>
  17. import Nav from '@/components/Nav'
  18. export default {
  19. methods: {
  20. goPage(path) {
  21. // 可以通过 this.$router 完成逻辑跳转
  22. this.$router.push();
  23. },
  24. goBack(num) {
  25. // 一般在移动端项目上运用
  26. this.$router.go(num);
  27. }
  28. },
  29. components: {
  30. Nav,
  31. }
  32. }
  33. </script>

组件传参

父传子

  1. 在子组件内通过 props 设置组件的自定义属性

      1. props: ['abc', 'goods']
  2. 在父组件渲染子组件时对自定义属性赋值即可

      1. <GoodsBox v-for="goods in goods_list" :abc="goods" :goods="goods"/>

子组件

  1. <template>
  2. <div class="goods-box">
  3. <img :src="goods.img" alt="">
  4. <p>{{ goods.title }}</p>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: "GoodsBox",
  10. // 在组件内部通过props定义组件的自定义属性
  11. props: ['abc', 'goods'],
  12. }
  13. </script>
  14. <style scoped>
  15. .goods-box {
  16. width: 260px;
  17. height: 300px;
  18. border: 1px solid black;
  19. border-radius: 5px;
  20. margin: 20px;
  21. float: left;
  22. overflow: hidden;
  23. text-align: center;
  24. }
  25. img {
  26. width: 260px;
  27. height: 260px;
  28. }
  29. </style>

父组件

  1. <template>
  2. <div class="goods">
  3. <div class="main">
  4. <!-- 在使用子组件时对自定义属性赋值即可 -->
  5. <GoodsBox v-for="goods in goods_list" :key="goods" :goods="goods" />
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import GoodsBox from "../components/GoodsBox";
  11. let goods_list = [
  12. {
  13. img: require('@/assets/img/001.jpg'),
  14. title: '小猫',
  15. },
  16. {
  17. img: require('@/assets/img/002.jpg'),
  18. title: '小猫儿',
  19. },
  20. {
  21. img: require('@/assets/img/003.jpg'),
  22. title: '小狗',
  23. },
  24. {
  25. img: require('@/assets/img/004.jpg'),
  26. title: '小狗儿',
  27. },
  28. ];
  29. export default {
  30. name: "Goods",
  31. data () {
  32. return {
  33. goods_list,
  34. }
  35. },
  36. components: {
  37. GoodsBox,
  38. },
  39. }
  40. </script>
  41. {
  42. img: require('@/assets/img/1.jpeg'),
  43. title: '1号',
  44. },
  45. {
  46. img: require('@/assets/img/2.jpeg'),
  47. title: '2号',
  48. },
  49. {
  50. img: require('@/assets/img/3.jpg'),
  51. title: '3号',
  52. },
  53. {
  54. img: require('@/assets/img/4.jpeg'),
  55. title: '4号',
  56. },

子传父

前提:子组件是被父组件渲染的,所以子组件渲染要晚于父组件

  1. 子组件一定要满足一个条件,才能对父组件进行传参(某个时间节点 === 某个被激活的方法)

    1. 子组件刚刚加载成功,给父组件传参
    2. 子组件某一个按钮被点击的时刻,给父组件传参 iii)子组件要被销毁了,给父组件传参
  2. 在子组件满足条件激活子组件的方法中,对父组件发生一个通知,并将数据携带处理(自定义组件事件)

      1. <div class="goods-box" @click="boxClick"></div>
      2. methods: {
      3. boxClick () { this.$emit('receiveData', this.goods.title, '第二个数据', '第三个数据') }
      4. }
  3. 在父组件渲染子组件时,为自定义事件绑定方法

      1. <GoodsBox @receiveData="recFn"/>
  4. 在父组件实现绑定方法时,就可以拿到子组件传参的内容(接收到了通知并在父组件中相应)

      1. recFn(title, data2, data3) {
      2. console.log('接收到了' + title);
      3. }

组件标签不能绑定系统定义的事件,没有意义,子组件的事件都是在自己内部完成

子组件

  1. <template>
  2. <div class="goods-box" @click="boxClick">
  3. <img :src="goods.img" alt="">
  4. <p>{{ goods.title }}</p>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: ['abc', 'goods'],
  10. methods: {
  11. boxClick () {
  12. // 通知父级 - 自定义组件的事件
  13. this.$emit('receiveData', this.goods.title)
  14. }
  15. }
  16. }
  17. </script>

父组件

  1. <template>
  2. <div class="goods">
  3. <div class="main">
  4. <!-- 实现自定义事件,接收子组件通知的参数 -->
  5. <GoodsBox v-for="goods in goods_list" @receiveData="recFn"/>
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import GoodsBox from "../components/GoodsBox";
  11. export default {
  12. name: "Goods",
  13. data () {
  14. return {
  15. goodsTitle: '哪个',
  16. }
  17. },
  18. methods: {
  19. recFn(title) {
  20. console.log('接收到了' + title);
  21. this.goodsTitle = title;
  22. }
  23. },
  24. components: {
  25. GoodsBox,
  26. },
  27. }
  28. </script>

组件的生命周期钩子

  1. 组件的生命周期:一个组件从创建到销毁的整个过程

  2. 生命周期钩子:在一个组件生命周期中,会有很多特殊的时间节点,且往往会在特定的时间节点完成一定的逻辑,特殊的事件节点可以绑定钩

    注:钩子 - 提前为某个事件绑定方法,当满足这个事件激活条件时,方法就会被调用 | 满足特点条件被回调的绑定方法就称之为钩子

  1. <template>
  2. <div class="goods">
  3. <Nav />
  4. </div>
  5. </template>
  6. <script>
  7. import Nav from "../components/Nav";
  8. export default {
  9. name: "Goods",
  10. components: {
  11. Nav,
  12. },
  13. beforeCreate() {
  14. console.log('该组件要被加载了')
  15. },
  16. created() {
  17. console.log('该组件要被加载成功了')
  18. },
  19. updated() {
  20. console.log('数据更新了')
  21. },
  22. destroyed() {
  23. console.log('该组件销毁了')
  24. }
  25. }
  26. </script>

路由传参

通过 url 正则传递数据

  1. 设置

      1. 路由:path: '/goods/detail/:pk' | '/goods/:pk/detail/:xyz'
      2. 请求:'/goods/detail/任意字符' | '/goods/任意字符/detail/任意字符'
  2. 如何传

      1. <router-link :to="`/goods/detail/${pk}`"></router-link>
      2. this.$router.push(`/goods/detail/${pk}`)
  3. 如何取

      1. this.$route对象是管理路由参数的,传递的参数会在this.$route.params字典中
      2. this.$route.params.pk

通过 url 参数传递数据

  1. 设置

    1. 路由: path: '/goods/detail'
    2. 请求: '/goods/detail?pk=数据'
  2. 如何传

    1. <router-link :to="`/goods/detail?pk=${pk}`"></router-link>
    2. <router-link :to="{name:'GoodsDetail', query:{pk: pk}}"></router-link>
    3. this.$router.push(`/goods/detail?pk=${pk}`)
    4. this.$router.push({name:'GoodsDetail', query:{pk: pk}})
  3. 如何取

    1. this.$route对象是管理路由参数的,传递的参数会在this.$route.query字典中
    2. this.$route.query.pk

第一种

配置:router/index.js

  1. const routes = [
  2. {
  3. path: '/goods/detail/:pk',
  4. name: 'GoodsDetail',
  5. component: GoodsDetail
  6. },
  7. ]

传递: GoodsBox.vue

  1. <router-link class="goods-box" :to="`/goods/detail/${goods.pk}`">
  2. <img :src="goods.img" alt="">
  3. <p>{{ goods.title }}</p>
  4. </router-link>
  5. <!------------------- 或者 ------------------->
  6. <div class="goods-box" @click="goDetail(goods.pk)">
  7. <img :src="goods.img" alt="">
  8. <p>{{ goods.title }}</p>
  9. </div>
  10. <script>
  11. export default {
  12. name: "GoodsBox",
  13. methods: {
  14. goDetail (pk) {
  15. this.$router.push(`/goods/detail/${pk}`);
  16. }
  17. }
  18. }
  19. </script>

接收:GoodsDetail.vue

  1. <script>
  2. export default {
  3. name: "GoodsDetail",
  4. data () {
  5. return {
  6. pk: '未知',
  7. }
  8. },
  9. // 通常都是在钩子中获取路由传递的参数
  10. created() {
  11. this.pk = this.$route.params.pk || this.$route.query.pk;
  12. }
  13. }
  14. </script>

第二种

配置:router/index.js

  1. const routes = [
  2. {
  3. path: '/goods/detail',
  4. name: 'GoodsDetail',
  5. component: GoodsDetail
  6. },
  7. ]

传递:GoodsBox.vue

  1. <router-link class="goods-box" :to="`/goods/detail?pk=${goods.pk}`">
  2. <img :src="goods.img" alt="">
  3. <p>{{ goods.title }}</p>
  4. </router-link>
  5. <!------------------- 或者 ------------------->
  6. <div class="goods-box" @click="goDetail(goods.pk)">
  7. <img :src="goods.img" alt="">
  8. <p>{{ goods.title }}</p>
  9. </div>
  10. <script>
  11. export default {
  12. name: "GoodsBox",
  13. methods: {
  14. goDetail (pk) {
  15. // this.$router.push(`/goods/detail?pk=${goods.pk}`);
  16. // 或者
  17. this.$router.push({
  18. name: 'GoodsDetail',
  19. query: {
  20. pk,
  21. }
  22. });
  23. }
  24. }
  25. }
  26. </script>

接收:GoodsDetail.vue

  1. <script>
  2. export default {
  3. name: "GoodsDetail",
  4. data () {
  5. return {
  6. pk: '未知',
  7. }
  8. },
  9. // 通常都是在钩子中获取路由传递的参数
  10. created() {
  11. this.pk = this.$route.params.pk || this.$route.query.pk;
  12. }
  13. }
  14. </script>

全家配置自定义 CSS 与 js

global.css

  1. html, body {
  2. margin: 0;
  3. }
  4. a {
  5. color: black;
  6. text-decoration: none;
  7. }
  8. ul {
  9. margin: 0;
  10. padding: 0;
  11. }

settings.js

  1. export default {
  2. base_url: 'https://127.0.0.1:8000'
  3. }

main.js

  1. //1) 配置全局css
  2. import '@/assets/css/global.css'
  3. // import global_css from '@/assets/css/global.css' // 资源需要用变量保存,方便以后使用
  4. // require('@/assets/css/global.css')
  5. // let global_css = require('@/assets/css/global.css') // 资源需要用变量保存,方便以后使用
  6. // 2) 配置自定义js设置文件
  7. import settings from '@/assets/js/settings.js'
  8. Vue.prototype.$settings = settings;
  9. // 在任何一个组件中的逻辑,可以通过 this.$settings访问settings.js文件的{}数据

总结:

  1. """
  2. 项目:
  3. 环境;node -> npm -> cnpm -> vue/cli
  4. 创建:vue create proj
  5. 配置:配置npm启动项
  6. 项目目录结构:依赖、环境、入口、核心代码们
  7. 组件:
  8. 构成:template + script + style
  9. 导入:import 别名 from '路径'
  10. 父加子:1)导入 2)注册 3)使用
  11. 组件数据:组件化处理 data(){ return {} }
  12. 传参:父传子 - 自定义组件属性 | 子传父 - 自定义组件事件
  13. 生命周期钩子:created() { //完成后台请求等 }
  14. 路由:
  15. 根组件中的页面占位:<router-view />
  16. 导航栏中的页面跳转:<router-link to=""></router-link>
  17. 代码中的逻辑跳转:this.$router.push() | this.$router.go()
  18. 路由传参:两种方式
  19. 两个路由对象:
  20. this.$router - 控制路由跳转
  21. this.$route - 控制路由数据
  22. """

Vue 路由组件的更多相关文章

  1. Vue路由组件vue-router

    一.路由介绍 Creating a Single-page Application with Vue + Vue Router is dead simple. With Vue.js, we are ...

  2. Vue 路由&组件懒加载(按需加载)

    当打包构建应用时,Javascript 包会变得非常大,影响页面加载速度.使用Vue路由懒加载和组件懒加载可以提升页面加载速度,减少白屏时间,提升用户体验. 用法有如下三种:(路由懒加载与组件懒加载用 ...

  3. vue路由组件传参

    在组件中使用 $route 会使之与其对应路由形成高度耦合,从而使组件只能在某些特定的 URL 上使用,限制了其灵活性. 使用 props 将组件和路由解耦: 取代与 $route 的耦合 const ...

  4. vue 路由组件不重新加载

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. vue路由组件群

    import Vue from 'vue'import VueRouter from 'vue-router'Vue.use(VueRouter) const first = { template: ...

  6. vue路由异步组件案例

    最近研究了vue性能优化,涉及到vue异步组件.一番研究得出如下的解决方案. 原理:利用webpack对代码进行分割是异步调用组件前提.异步组件在优先级上让位同步组件.下面介绍的是怎么实现异步组件. ...

  7. 067——VUE中vue-router之使用transition设置酷炫的路由组件过渡动画效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Vue.js—组件快速入门及Vue路由实例应用

    上次我们学习了Vue.js的基础,并且通过综合的小实例进一步的熟悉了Vue.js的基础应用.今天我们就继续讲讲Vue.js的组件,更加深入的了解Vue,js的使用.首先我们先了解一下什么是Vue.js ...

  9. vue路由懒加载及组件懒加载

    一.为什么要使用路由懒加载 为给客户更好的客户体验,首屏组件加载速度更快一些,解决白屏问题. 二.定义 懒加载简单来说就是延迟加载或按需加载,即在需要的时候的时候进行加载. 三.使用 常用的懒加载方式 ...

随机推荐

  1. django的静态文件配置和路由控制

    上一篇写到刚建完django项目,此时我登录页面中调用了js文件,执行后发现报错了找不到js这个文件 目录结构如图所示: <!DOCTYPE html> <html lang=&qu ...

  2. python匿名函数与三元运算

      匿名函数 匿名函数就是不需要显示式的指定函数名 首先看一行代码: def calc(x,y): return x*y print(calc(2,3)) # 换成匿名函数 calc = lambda ...

  3. Python 命令行参数的输入方式(使用pycharm)

    形式一: 第一个红色框为命令行参数 第二个框为控制台模式(Terminal) 形式二 点击运行:点击红色框  编辑配置 如下图:红色框即为设置的命令行参数

  4. 兵贵神速!掌握这10个Python技巧,让你代码工作如鱼得水

    主题 Python 1000个读者心中有1000个哈姆雷特,要问1000个程序员“什么才是最好的语言”,Java.Python.PHP.C++ 也都有自己的位置.但要问编程语言流行指数之王非,那真的非 ...

  5. 关于OSPF LSA不稳定!

    Issue 1 Solution It is important that you understand the error message during attempts to troublesho ...

  6. 转专业后对于C语言补修的一些体会(2)

    第三章,有以下几个比较重要的点: 1. 强制类型转换. 强制类型转换是C语言中一个十分重要的工具,在C语言的使用中,有很多需要用到强制类型转换的地方,比如在除法中,如果想要得到正确的浮点结果,一般要确 ...

  7. Pipenv & 虚拟环境

    本教程将引导您完成安装和使用 Python 包. 它将向您展示如何安装和使用必要的工具,并就最佳做法做出强烈推荐.请记住, Python 用于许多不同的目的.准确地说,您希望如何管理依赖项可能会根据 ...

  8. org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown database 'jdsc')

    org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown databas ...

  9. Win Tomcat8 占用内存过高

    1.解压版 找到tomcat/bin/catalina.bat 文件,修改对应参数 2.安装版 windows服务执行的是bin/tomcat.exe.他读取注册表中的值,而不是catalina.ba ...

  10. iOS马甲包上架总结

    https://www.jianshu.com/p/da0a259338ea iOS马甲包上架首先明白一点,这个上架的app马甲包一定是不合规的.不然也不会使用马甲包上架. 上架过程中遇到的坑. 因为 ...