提供/注入 @vue/composition-api 不更新

时间:2021-03-25 16:51:06

标签: vuejs2 vue-composition-api

请注意,我使用的不是 vue 3,而是 @vue/composition-api 和 vue 2。

我的路由器有以下内容:

private static bool FindRecord(Expression expression, IEnumerable<int> list)
{
    ParameterExpression parameter = Expression.Parameter(typeof(int), "x");
    Expression body = Expression.Equal(parameter, Expression.Invoke(expression));
    Expression<Func<int, bool>> lambda = (Expression<Func<int, bool>>)Expression.Lambda(body, parameter);
        
    Console.WriteLine(lambda);
    return list.Any(lambda.Compile());
}

我的根组件有:

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

更改路线时,我确实收到日志说它已更改,但在我使用注入的任何组件中都不会更新:

setup(props) {
  const loc=ref(props.locale);
  watch(()=>props.locale,
  (current)=>{
    // eslint-disable-next-line no-console
    console.log('in root watch',current);
    loc.value=current
  }
  )
  provide('locale', loc);
},

1 个答案:

答案 0 :(得分:0)

我用错了手表,在我应该做的孩子身上:

setup(){
  const locale = inject('locale')
  watch(
    locale,//not a function but an observable
    (...args)=>{
      //works
      console.log('args:',args)
    }
  );
},