优秀的编程知识分享平台

网站首页 > 技术文章 正文

Vue进阶(七十三):commits与dispatch

nanyue 2024-10-17 11:22:45 技术文章 7 ℃

代码示例:

this.$store.commit('loginStatus', 1);
this.$store.dispatch('isLogin', true);

规范的使用方式:

// 以载荷形式
store.commit('increment',{
  amount: 10   //这是额外的参数
})
// 或者使用对象风格的提交方式
store.commit({
  type: 'increment',
  amount: 10   //这是额外的参数
})

主要区别:

  • dispatch:含有异步操作,数据提交至 actions ,可用于向后台提交数据

写法示例: this.$store.dispatch('isLogin', true);

  • commit:同步操作,数据提交至 mutations ,可用于登录成功后读取用户信息写到缓存里

写法示例: this.$store.commit('loginStatus', 1);

两者都可以以载荷形式或者对象风格的方式进行提交。

拓展阅读 关于js中页面跳转的用法

“window.location.href”、"location.href"是本页面跳转.
“parent.location.href” 是上一层页面跳转.
“top.location.href” 是最外层的页面跳转.

举例说明:

如果A,B,C,D都是html,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js这样写

"window.location.href"、"location.href":D页面跳转
"parent.location.href":C页面跳转
"top.location.href":A页面跳转

如果D页面中有form的话,

<form>:  form提交后D页面跳转
<form target="_blank">:  form提交后弹出新页面
<form target="_parent">:  form提交后C页面跳转
<form target="_top"> :  form提交后A页面跳转

如果访问的是iframe里面的页面,重新加载最外层的页面。

最近发表
标签列表