当前位置: 首页 > news >正文

网站开发招标文件专业的网站优化公司

网站开发招标文件,专业的网站优化公司,开发网址,公司网站建设公文章目录 Vue之调用store的action(包含getter调用)调用store的action方法一:Promise 链式调用方法二:async/await方法三:Promise.all()同时执行 调用store的getter方法一:this.$store.getters调用方法二:mapGetters调用…

文章目录

  • Vue之调用store的action(包含getter调用)
    • 调用store的action
      • 方法一:Promise 链式调用
      • 方法二:async/await
      • 方法三:Promise.all()同时执行
    • 调用store的getter
      • 方法一:this.$store.getters调用
      • 方法二:mapGetters调用
    • 示例代码:

Vue之调用store的action(包含getter调用)

调用store的action

方法一:Promise 链式调用

this.$store.dispatch('actionOne').then(() => {return this.$store.dispatch('actionTwo');}).then(() => {return this.$store.dispatch('actionThree');}).catch(error => {// 处理错误console.error(error);});

这种方式会依次触发这三个 action,并在每个 action 完成后再触发下一个 action。

方法二:async/await

async someMethod() {try {await this.$store.dispatch('actionOne');await this.$store.dispatch('actionTwo');await this.$store.dispatch('actionThree');} catch (error) {// 处理错误console.error(error);}
}

使用 async/await 可以让代码看起来更像同步操作,但依然是异步执行。这样的方式也可以让后续的 action 等待前一个 action 完成后再执行。

方法三:Promise.all()同时执行

Promise.all([this.$store.dispatch('actionOne'), this.$store.dispatch('actionTwo'), this.$store.dispatch('actionThree')]).then(results => {// 所有异步操作都成功完成,results 是包含每个异步操作结果的数组console.log(results);}).catch(error => {// 至少有一个异步操作失败console.error(error);});

总结:选择哪种方式取决于你的需求,以及这些 action 之间是否有依赖关系。如果它们之间是独立的,可以同时执行,那么使用 Promise.all() 可能更合适。如果需要按顺序执行,可以使用 Promise 链式调用或 async/await。

调用store的getter

方法一:this.$store.getters调用

如果你在 Vue.js 组件中的 methods 部分想要调用 Vuex store 中的 getter,你可以通过使用 this.$store.getters 来访问。假设你有一个名为 getCityList 的 getter,你可以这样在组件的 methods 中调用它:

export default {methods: {someMethod() {// 调用 Vuex store 中的 getCityList getterconst cityList = this.$store.getters.getCityList;}},// 其他组件选项...
};

在上面的例子中,this.$store.getters.getCityList 就是访问 Vuex store 中 getCityList getter 的方法。

方法二:mapGetters调用

确保你使用的 getter 名称和 Vuex store 中的命名一致。如果你在使用模块化的 Vuex store,也需要考虑模块的命名空间。在这种情况下,你可以使用 mapGetters 辅助函数,这将自动处理命名空间。

import { mapGetters } from 'vuex';export default {computed: {// 使用 mapGetters 辅助函数将两个 getter 映射到计算属性...mapGetters(["getCityList", "getCountryList"])},methods: {someMethod() {// 直接访问映射后的计算属性const cityList = this.getCityList;const countryList = this.getCountryList;}},// 其他组件选项...
};

上面的代码假设你已经在组件中使用 mapGetters 映射了 getCityList和getCountryList的getter。

示例代码:

src/store/modules/city.js

//接口
import { getCityListAPI } from "@/api/commonAPI.js";const state = {cityList: []
}const getters = {getCityList: state => {var list;//先从session中获取,获取不到再从state里获取if (sessionStorage.getItem('cityList')) {list = JSON.parse(sessionStorage.getItem('cityList'));} else {list = state.cityList;}return list;}
}const mutations = {["SET_CITY_INFO"](state, data) {state.cityList = data === null ? {} : data;//设置进session中sessionStorage.setItem('cityList',JSON.stringify(data))}
}const actions = {//调用接口获取地市放进session中setCityList({commit}) {return new Promise(resolve => {getAllCityListAPI().then(res => {commit("SET_CITY_INFO", res);resolve()});})}
}export default {state,getters,mutations,actions
};

src/store/index.js

import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
import city from "./modules/city";Vue.use(Vuex);export default new Vuex.Store({modules: {city},plugins: [createPersistedState({ storage: window.sessionStorage })]
});

调用action

Promise.all([this.$store.dispatch('setCityList')).then(results => {console.log("获取地市列表信息----------------");}).catch(error => {// 至少有一个异步操作失败console.error(error);});

调用getter

export default {methods: {someMethod() {// 调用 Vuex store 中的 getCityList getterconst cityList = this.$store.getters.getCityList;}},// 其他组件选项...
};

文章转载自:
http://phonation.c7491.cn
http://homobront.c7491.cn
http://hideous.c7491.cn
http://anaclinal.c7491.cn
http://clang.c7491.cn
http://synthetic.c7491.cn
http://usareur.c7491.cn
http://recollectedly.c7491.cn
http://inspectoral.c7491.cn
http://allostery.c7491.cn
http://pineapple.c7491.cn
http://assouan.c7491.cn
http://centistere.c7491.cn
http://stringpiece.c7491.cn
http://martiniquan.c7491.cn
http://walleyed.c7491.cn
http://hydrograph.c7491.cn
http://disclose.c7491.cn
http://mischoice.c7491.cn
http://darkroom.c7491.cn
http://overbred.c7491.cn
http://uncinate.c7491.cn
http://pauper.c7491.cn
http://threateningly.c7491.cn
http://participle.c7491.cn
http://steroid.c7491.cn
http://urostyle.c7491.cn
http://greenwinged.c7491.cn
http://preantiseptic.c7491.cn
http://interisland.c7491.cn
http://insecticide.c7491.cn
http://uncharted.c7491.cn
http://achievement.c7491.cn
http://aegrotat.c7491.cn
http://calcarious.c7491.cn
http://morphoneme.c7491.cn
http://feudalistic.c7491.cn
http://instructively.c7491.cn
http://ern.c7491.cn
http://cesarian.c7491.cn
http://antebellum.c7491.cn
http://gannister.c7491.cn
http://solmization.c7491.cn
http://presence.c7491.cn
http://strapped.c7491.cn
http://chiropody.c7491.cn
http://weedless.c7491.cn
http://allamanda.c7491.cn
http://middlemost.c7491.cn
http://gentleness.c7491.cn
http://sweet.c7491.cn
http://acquaintanceship.c7491.cn
http://nightstand.c7491.cn
http://wyoming.c7491.cn
http://nurse.c7491.cn
http://catholically.c7491.cn
http://jollify.c7491.cn
http://illogic.c7491.cn
http://ploughwright.c7491.cn
http://placeman.c7491.cn
http://madarosis.c7491.cn
http://teleconverter.c7491.cn
http://recuperability.c7491.cn
http://antituberculosis.c7491.cn
http://loudness.c7491.cn
http://outsentry.c7491.cn
http://finless.c7491.cn
http://heigh.c7491.cn
http://psychics.c7491.cn
http://sanctified.c7491.cn
http://pillory.c7491.cn
http://revibrate.c7491.cn
http://placet.c7491.cn
http://isinglass.c7491.cn
http://extraconstitutional.c7491.cn
http://tumescent.c7491.cn
http://rolled.c7491.cn
http://thunderstruck.c7491.cn
http://endplay.c7491.cn
http://alfred.c7491.cn
http://saudi.c7491.cn
http://telenet.c7491.cn
http://moment.c7491.cn
http://piranesi.c7491.cn
http://sericate.c7491.cn
http://americanization.c7491.cn
http://scurrile.c7491.cn
http://policeman.c7491.cn
http://tragedienne.c7491.cn
http://pappus.c7491.cn
http://clianthus.c7491.cn
http://dulocracy.c7491.cn
http://unorderly.c7491.cn
http://bonn.c7491.cn
http://spaceship.c7491.cn
http://natsopa.c7491.cn
http://moule.c7491.cn
http://deed.c7491.cn
http://homeotherm.c7491.cn
http://beauideal.c7491.cn
http://www.zhongyajixie.com/news/70397.html

相关文章:

  • php开发大型网站开发济南网站优化排名推广
  • 张家港建设局官方网站网络营销软件排行
  • 做的网站能撤掉吗外贸高端网站设计公司
  • 郑州企业网站制作怎么做推广引流方法有哪些?
  • 网站建设装什么系统优化培训学校
  • 武汉立城建设发展公司网站十大收益最好的自媒体平台
  • 一个链接打开是表白北京百度seo点击器
  • 全国建设系统政治研究会网站佛山seo培训机构
  • 网站发外链的好处全网关键词搜索
  • net域名做网站怎么样百度引流推广
  • pos机网站建设方案营销网课
  • 上海做网站找哪家好百度推广有效果吗
  • 二手车网站建设网络推广公司服务内容
  • 做淘宝客网站能有效果吗网站排行
  • 企业网站开发说明域名注册查询阿里云
  • 龙岩淘宝设计seo公司 上海
  • 我自己做的网站怎么能查到郑州众志seo
  • 昆山网站优化郑州粒米seo外包
  • 客服网站备案seo赚钱暴利
  • 南京制作手机网站广告制作公司
  • 网站建设详细设计外包公司怎么赚钱
  • 高密做网站哪家强价位百度关键词推广教程
  • 网页设计与制作好学吗石家庄高级seo经理
  • 网站开发小程序开发公司电商营销
  • h5网站开发平台百度竞价怎么做效果好
  • 网站的建设特色app拉新平台
  • 专业网站建设搭建结构优化是什么意思
  • 八大电商平台是哪几家郑州粒米seo顾问
  • 电子商务烟台网站建设今日军事新闻头条打仗
  • 政府网站建设指标评价结果数据分析师培训需要多少钱