首页 > 其他分享 >【Vue项目】尚品汇(四)Search组件开发

【Vue项目】尚品汇(四)Search组件开发

时间:2022-09-01 16:24:12浏览次数:44  
标签:Search Vue const searchInfoList route searchParams 尚品 state params

Search模块开发

分析:1)编写静态页面

2)编写api

3)编写vuex三大件

4)组件获取仓库数据,并进行动态展示

1 SearchSelector

1 编写api
export const reqGetSearchInfo = (params={}) => {
    return requests({
        url: '/list',
        method: 'post',
        data: params
    })
}
2 编写Vuex三大件
import {reqGetSearchInfo} from "@/api";
const actions = {
    async getSearchInfo(context, params) {
        let result = await reqGetSearchInfo(params)
        if(result.code == 200) {
            context.commit('GETSEARCHINFO', result.data);
        }
    }
}
const mutations = {
    GETSEARCHINFO(state, searchInfoList) {
        state.searchInfoList = searchInfoList
    }
}
const getters = {

}
const state = {
    searchInfoList: {}
}

// 将模块仓库内容对外暴露
export default {
    state,
    actions,
    mutations,
    getters
}

注意state仓库的数据是对象{}还是数组[]是由返回的数据决定的。

3 组件获取仓库数据,并进行动态展示
    mounted() {
      this.$store.dispatch('getSearchInfo');
    },
    computed: {
      ...mapState({
        SearchInfoList: (state) => {
          return state.search.searchInfoList
        }
      })
    }
4 监听路由变化再次发送请求数据

之前通过三级联动的query参数整合了搜索框中的params参数,当点击三级联动的时候能够带上搜索框的参数,当点击搜索框的时候能带上三级联动的参数。但是在进入搜索页面之后并不能继续进行发出请求,这是因为我们只在mounted里面进行了整理参数、发出请求(实际是在vuex的action中)。

因此我们可以通过监听路由的变化,当路由变化的时候重新进行参数整理和请求发送即可。

<script>
  import SearchSelector from './SearchSelector/SearchSelector'
  import {mapState} from "vuex";
  export default {
    name: 'Search',
    components: {
      SearchSelector
    },
    data() {
      return {
        searchParams: {

        }
      }
    },
    methods: {
      getData() {
        this.$store.dispatch('getSearchInfo', this.searchParams);
      }
    },
    mounted() {
      this.getData();
    },
    beforeMount() {
      Object.assign(this.searchParams, this.$route.query, this.$route.params)
    },
    computed: {
      ...mapState({
        SearchInfoList: (state) => {
          return state.search.searchInfoList
        }
      })
    },
    watch: {
      $route() {
        console.log("123")
        Object.assign(this.searchParams, this.$route.query, this.$route.params)
        this.getData()
        this.searchParams = {}
      }
    }
  }
</script>

各种请求接口参数一定要注意参照接口文档。。。

对路由的监视一定要加上 this.searchParams = {},防止在重复点击三级联动的时候会携带上次的三级分类的剩余信息

从后面回来的,这里还真不能直接这么写,因为后面面包屑会用到categoryName,否则再次点击三级联动会导致面包屑不显示。应该改为:

      $route() {
        Object.assign(this.searchParams, this.$route.query, this.$route.params)
        console.log(this.searchParams)
        this.getData()

        this.searchParams.category1Id = ''
        this.searchParams.category2Id = ''
        this.searchParams.category3Id = ''
      }

2 面包屑

3 平台售卖属性

4 商品排序

5 分页器

分页器需要的参数:

  • 当前的页码:pageNo

  • 每页的数据:pageSize

  • 分页器的总页数:total

  • 分页器连续页码个数:continue

标签:Search,Vue,const,searchInfoList,route,searchParams,尚品,state,params
From: https://www.cnblogs.com/tod4/p/16646905.html

相关文章

  • vue3 + NaiveUI Modal组件点击右上角x关闭不了弹窗的问题
    不要使用v-modle:show='props.show'的方式。因为使用v-modle后,会警告不能直接修改父组件的值,只是可读的应该采用::show='props.show'@update:show='changeShow'配合@......
  • vue在图片上打点功能+旋转摄像头
    <template>  <divclass="point">    <divclass="mongolia"id="mongolia"@click.stop="creat_point">      <divref="testDom"class="ma......
  • Vue基础5个实用案例
    Vue基础5个实用案例前言本章节怼几个案例供读者小伙伴们练习,写不出东西就是写的少,多写就有思路,案例也懒得去搞CSS了,大家主要锤Vue就可以了。不废话直接上货!案例1:选择登陆......
  • vue3项目-小兔鲜儿笔记-首页04
    1.新了解CSS属性:object-fitobject-fit属性对图片进行剪切,保留原始比例一般用于img标签和video标签。object-fit属性值:fill默认,不保证保持原有比例,内容拉伸......
  • vue项目中main.js使用方法详解
    vue项目中main.js使用方法详解目录第一部分:main.js文件解析第二部分:Vue.use的作用以及什么时候使用Vue.use是什么?(官方文档)Vue.use()什么时候使用?补充:关于main.js方便小技......
  • vue方法中的方法怎么同步顺序执行_vue方法同步(顺序)执行:async/await使用 , 使用async搭
    vue方法中的方法怎么同步顺序执行_vue方法同步(顺序)执行:async/await使用项目中有一个地方需要获取到接口返回值之后根据返回值确定之后执行的步骤,使用async搭配await实......
  • 在vue中进行ElementUI 组件的安装、引入 npm i element-ui -S
    在vue中进行ElementUI组件的安装、引入1.安装、引入elementUIcmd进入vue项目所在文件夹安装elementUInpmielement-ui-S在main.js中引入importElementUI......
  • vue 项目优化
    生成打包报告(vueui可视化面板)通过vue.config.js修改webpack的默认配置 (①chainWebpack通过链式编程的形式,来修改默认的webpack配置②configureWebpa......
  • vue 多页面应用
    1,认识vue页面加载过程在创建多页面之前,还是要先简单地了解一下,vue页面时怎么加载的。我们知道,在Vue-cli中默认目录结构如下:1.项目的依赖模块目录,这个目录很大,因此一......
  • [Vue warn]: Avoid mutating a prop directly since the value will be overwritten w
    报错提示:vue.runtime.esm.js?2b0e:619[Vuewarn]:Avoidmutatingapropdirectlysincethevaluewillbeoverwrittenwhenevertheparentcomponentre-renders.I......