vue3开发问题集

881 阅读1分钟

1. keep-alive包裹动态组件没生效

后台管理项目,路由配置要兼容菜单生成,有两级,在第一个router-view下的component外包裹keep-alive,如下:

<router-view v-slot="{Component}">
<transition name="fade-transform" mode="out-in">
  <keep-alive>
    <component :is="Component" />
  </keep-alive>
</transition>
</router-view>

路由切换一直没生效,在网上查了很多资料,但是看自己的配置确实是对的,没有意识到。。。 最后安装了dev-tools工具,发现keep-alive下还有两层组件才是我需要缓存的组件,于是研究router-view,上面的router-view对应路由配置的第一层,如下:

{
    path: '/material',
    name: '素材管理',
    redirect: '/material/list',
    component: Wrapper,
    meta: {
      icon: 'el-icon-s-order',
    },
    children: [
      {
        path: 'list',
        name: '资源位内容列表',
        component: MaterialList,
      },
      {
        path: 'add',
        name: '资源位内容新增',
        component: MaterialAdd,
        meta: {
          cache: true,
          componentName: 'ContentAdd',
        },
      },
    ],
  },

对应的组件是Wrapper:

<router-view />

即上面的component对应这个Wrapper,并不是我需要缓存的二级路由组件,所以需要把keep-alive移到Wrapper内!

排查这个问题效率有点低,得到的教训是:以后要好好读懂api,真正理解它,利用好工具(dev-tools),发现问题所在才能解决它!

2. 单文件组件的开发形式,组件名称可以动态获取吗?

在route配置中定义组件名称,在单文件组件中怎么拿到并定义当前组件名称?

3. el-input不显示初始值?

<el-input :value="inputValue" disabled />

const inputValue = ref('');
...
inputValue.value = v;

需要改成v-model才行

4. 不同路由配置对应同一组件,切换路由时组件不刷新

1) onBeforeRouteUpdate -失败
2) watch route 通过v-if控制组件重新渲染 -失败
3) 路由组件增加key: route.fullPath

5. setup模式一定注意不要解构使用props!