useRoute 未定义

useRoute is undefined

我正在尝试在 vue 2.6 应用程序中使用 useRoute,但它未定义:

import { useRoute } from 'vue-router'
console.log('useRoute is',useRoute)

我的package.json:

"@vue/composition-api": "^1.0.0-rc.5",
"vue": "^2.6.12",
"vue-router": "^3.4.8",

我在 documentation 中找不到任何告诉我这行不通的内容。

我可以看到当前版本是 4,但这取决于 vue 3我没有那个版本,担心更新时一切都会崩溃。并且不需要这种依赖,因为我安装了合成 api。

就像大多数 vue 的“文档”一样,useRoute api 文档似乎丢失了,因为此功能可用的版本。

vue router only in vue router next I know both pages look like they are for vue-router and none of the next content would suggest it's not for vue-router but the url indicates it's for vue router next中没有useRouter。

很高兴为未来做好准备,但目前在执行 yarn add vue 时仍然安装了 vue 2,所以如果文档有一个 header 表明这是完全不同的,那就太好了恰好具有相同名称和徽标的项目。

我怀疑正在查看 this question 我在 composition-api 设置函数中尝试获取 vue-route 参数时运气不佳。

更新

使用 provide/inject 我可以从每个组件获取路由道具:

{
  path: `/:country(${
    Object.keys(config.countries).join('|')
  })?/:locale(${
    Object.keys(config.languages).join('|')
  })?`,
  props:true,//need to have this
  component: Root,

我的根组件有:

props: {
  locale: String,//this is mandatory
},
setup(props) {
  const loc=ref(props.locale);
  watch(()=>props.locale,
  (current)=>{
    loc.value=current
  }
  )
  provide('locale', loc);
},

在 child 个组件中:

setup(){
  // locale is an observable
  const locale = inject('locale')
},