vue icon indicating copy to clipboard operation
vue copied to clipboard

How to better cooperate with Vuex and Vue Router in <script setup></script> in 2.7

Open maomincoding opened this issue 1 year ago • 15 comments

What problem does this feature solve?

Hello!How to better cooperate with Vuex and Vue Router in in 2.7?

maomincoding avatar Jul 12 '22 13:07 maomincoding

Vuex: Migrate to pinia Router: https://github.com/vuejs/vue-router/issues/3760

kingyue737 avatar Jul 12 '22 16:07 kingyue737

@kingyue737 Thanks for your advice.

maomincoding avatar Jul 13 '22 03:07 maomincoding

I developed this simple solution. Maybe it could be useful to someone else.

https://gist.github.com/hugo-cardoso/7c8da4f55d521a6288fc7a0f7d2ff06c

hugo-cardoso avatar Jul 29 '22 05:07 hugo-cardoso

Does anyone have a solution for using Vuex that does not include Pinia and keeps the store calls the same as Vue3?. Hugo's works but I think it changes the syntax too much.

jkurtz678 avatar Aug 21 '22 23:08 jkurtz678

Vue router has released version 3.6 to support Vue 2.7: https://github.com/vuejs/vue-router/blob/dev/CHANGELOG.md

Vuex:

import  {  getCurrentInstance } from 'vue'
export function useStore() {
  const vm = getCurrentInstance()
  return vm.proxy.$store
}

useStore() can only be used in setup.

YFengFly avatar Aug 23 '22 02:08 YFengFly

For some reason, when importing using the conventional form:

import { useRoute } from "vue-router";

I get this message in the build (and thus, the app cannot work properly): export 'useRoute' was not found in 'vue-router'

I'm using

  • vue-router 3.6.3
  • vue: 2.7.10

Am I misisng something?

vate avatar Aug 24 '22 08:08 vate

@vate https://github.com/vuejs/vue-router/blob/dev/CHANGELOG.md#360-2022-08-22 import { useRoute, useRouter, useLink, onBeforeRouteUpdate onBeforeRouteLeave } from 'vue-router/composables'

YFengFly avatar Aug 24 '22 08:08 YFengFly

import { useRoute, useRouter, useLink, onBeforeRouteUpdate onBeforeRouteLeave } from 'vue-router/composables'

Already tried, but I'm getting this message:

This dependency was not found:

* vue-router/composables in ./node_modules/cache-loader/dist/cjs.js??ref--13-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--1-0!./node_modules/vue-loader/lib?[...]
  • using vue-cli 4.5.19 (may this be the problem?)

vate avatar Aug 24 '22 08:08 vate

In package.json of vue-router 3.6.3:

  "exports": {
    "./composables": {
      "import": "./dist/composables.mjs",
      "require": "./dist/composables.js",
      "types": "./types/composables.d.ts"
    },
  },

In vue-cli 4.5.15, it support for *.mjs is already available. In Webpack 5, it support Package exports, but in Webpack 4, I don't find it. I think you should go to vue-router and create a new issue. @vate

YFengFly avatar Aug 24 '22 09:08 YFengFly

Thanks for the hint! 🙌🏻

For the record, I managed to make it work using

import { useRoute } from "vue-router/dist/composables.js"

Not the cleanest solution, so I'll follow your advice to post a comment in vue-router.

UPDATE:

Using

import { useRoute } from "vue-router/composables"

worked fine for me on version 3.6.4

vate avatar Aug 24 '22 10:08 vate

I'm glad I could give you some ideas.But after looking at router's package.json and its source code,I think we should use the *.mjs file,which is:

import { useRoute } from "vue-router/dist/composables.mjs"

If this works,then this is more recommended.

Update: router 3.6.4 has supported webpack 4 https://github.com/vuejs/vue-router/blob/dev/CHANGELOG.md#364-2022-08-25

YFengFly avatar Aug 25 '22 03:08 YFengFly

@YFengFly I tried the useStore that you suggested in https://github.com/vuejs/vue/issues/12651#issuecomment-1223450798, it worked, but I was not able to listen to changes from the store, I used computed, but it doesn't update when the store gets updated

const userData = computed(() => store.getters['authentication/userData'])

Any suggestions here?

mohammedzamakhan avatar Oct 21 '22 04:10 mohammedzamakhan

@mohammedzamakhan Sorry for not replying for so long! this is very weird, I have tried computed, but nothing works. I wanted to reply after finding a solution, but I don't have a good idea at present. feel very sorry

YFengFly avatar Oct 31 '22 09:10 YFengFly

I think Vue core team should provide clear and detailed guide in terms of how to use vuex@4 and vue-router@4 with [email protected], if Composition API is supported in [email protected] then other official packages should support this as well

humoyun91 avatar Nov 01 '22 02:11 humoyun91