• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

菜鸡吃糖君

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

vue2-3随笔 组件化/脚手架2

1.Vue封装的过度与动画

  作用:在插入、更新或移除 DOM元素时,在合适的时候给元素添加样式类名。

  使用:

   1. 准备好样式:

    - 元素进入的样式

      1. v-enter:进入的起点 

      2. v-enter-active:进入过程中

      3. v-enter-to:进入的终点

    - 元素离开的样式:

      1. v-leave:离开的起点

      2. v-leave-active:离开过程中

      3. v-leave-to:离开的终点

   2. 使用```<transition>```包裹要过度的元素,并配置name属性:name属性与前文中样式名中的v相同  例如  ‘name’-leave

   3. 备注:若有多个元素需要过度,则需要使用:```<transition-group>```,且每个元素都要指定```key```值。

2.跨域问题的解决:代理服务器

  在vue.config.js中添加如下配置:devServer:{proxy:"http://localhost:5000}

    1. 优点:配置简单,请求资源时直接发给前端(8080)即可。

    2. 缺点:不能配置多个代理,不能灵活的控制请求是否走代理。

    3. 工作方式:若按照上述配置代理,当请求了前端不存在的资源时,那么该请求会转发给服务器 (优先匹配前端资源)

  其二: module.exports = { devServer: { proxy: {  '/api1': {// 匹配所有以 '/api1'开头的请求路径  target: 'http://localhost:5000',// 代理目标的基础路径   changeOrigin: true,  pathRewrite: {'^/api1': ''}  },

                        '/api2': {// 匹配所有以 '/api2'开头的请求路径   target: 'http://localhost:5001',// 代理目标的基础路径   changeOrigin: true,  pathRewrite: {'^/api2': ''}}}}

  注明:changeOrigin设置为true时,服务器收到的请求头中的host为:localhost:5000 ;changeOrigin设置为false时,服务器收到的请求头中的host为:localhost:8080;changeOrigin默认值为true

    优点:可以配置多个代理,且可以灵活的控制请求是否走代理。

    缺点:配置略微繁琐,请求资源时必须加前缀。

3.插槽的使用:slot

  作用:让父组件可以向子组件指定位置插入html结构,也是一种组件间通信的方式,适用于 父组件 向 子组件,共三种默认插槽、具名插槽、作用域插槽

  默认插槽:

    父组件中:  <Category>  <div>html结构1</div>   </Category>

    子组件中:  <template>  <div>   <!-- 定义插槽 -->  <slot>插槽默认内容...</slot>  </div>  </template>

  具名插槽:

    父组件中:<Category>   <template slot="center">    <div>html结构1</div>    </template>   <template v-slot:footer>   <div>html结构2</div>   </template>   </Category>

    子组件中:<template>    <div>    <!-- 定义插槽 -->    <slot name="center">插槽默认内容...</slot>    <slot name="footer">插槽默认内容...</slot>   </div>    </template>

  作用域插槽:

    1. 理解:<span style="color:red">数据在组件的自身,但根据数据生成的结构需要组件的使用者来决定。</span>(games数据在Category组件中,但使用数据所遍历出来的结构由App组件决定)

    2. 具体编码:

      父组件中:<Category>   <template scope="scopeData">    <!-- 生成的是ul列表 -->  <ul>    <li v-for="g in scopeData.games" :key="g">  {{g}}  </li>   </ul> </template></Category>

             <Category>   <template slot-scope="scopeData">   <!-- 生成的是h4标题 -->   <h4 v-for="g in scopeData.games" :key="g">   {{g}}  </h4>    </template>    </Category>

      子组件中:<template>  <div>  <slot :games="games">  </slot>  </div>  </template>

              <script>   export default {    name:'Category',   props:['title'],  //数据在子组件自身data() {return {games:['红色警戒','穿越火线','劲舞团','超级玛丽']}},}   </script>

4.状态管理器Vuex

  概念:在Vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读/写),也是一种组件间通信的方式,且适用于任意组件间通信。

  (1)创建文件

    import Vue from 'vue'    

    import Vuex from 'vuex'       

    Vue.use(Vuex)         

    const actions = {}  //响应组件中用户的动作,分发任务给actions或mutations

    const mutations = {}  //修改state中的数据

    const state = {}   //存储数据

    const getters = {}  //获取数据

    export default new Vuex.Store({

      actions,  mutations, state ,getters  })   //创建并暴露store

  (2)在main.js中引入

    import store from './store'

    new Vue({

      el:'#app',

      render: h => h(App),

      store })

  (3)简单读取state中的数据 :  $store.state.sum

      需要对读取的数据进行处理:     $store.getters.bigSum(函数名)  

      简单修改state中的数据:        $store.commit('mutations中的方法名',数据)

      当修改的流程相对复杂,设计逻辑判断:   $store.dispatch('action中的方法名',数据)  ,然后由actions再向mutations发送任务

  (4)Vuex的四个map

    ...mapState/mapGetters/mapActions/mapMutations  ({sum:'sum',school:'school',subject:'subject'}),

    ...mapState/mapGetters/mapActions/mapMutations(['JIA','JIAN'])

    备注:mapActions与mapMutations使用时,若需要传递参数需要:在模板中绑定事件时传递好参数,否则参数是事件对象。

  (5)自命名空间

    书写形式:

      const countAbout = {

        namespaced:true,//开启命名空间

        state:{x:1},

        mutations: { ... },

        actions: { ... },

        getters: { ... }   }

      // 省略 personAbout   

      const store = new Vuex.Store({

        modules: {

          countAbout,   personAbout   }   })

    使用变化

      (1)state:  this.$store.getters['personAbout/firstPersonName']        //  另一种写法     ...mapGetters('countAbout',['bigSum'])

      (2)getters:   this.$store.getters['personAbout/firstPersonName']     //  ...mapGetters('countAbout',['bigSum'])

      (3)actions/mutations:  this.$store.dispatch('personAbout/addPersonWang',person)     // ...mapActions('countAbout',{incrementOdd:'jiaOdd',incrementWait:'jiaWait'})

posted on 2022-03-16 16:17  菜鸡吃糖君  阅读(43)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3