Vue 组件函数如何实现可重用?

第一卷

在 Vue 应用程序中,我有许多功能只能在用户主动登录时使用。

检查用户是否通过身份验证的函数如下所示:

    import {useStore} from "vuex";
    import {useRoute, useRouter} from "vue-router";   
        
    export default {
     setup() {
        const VueStore = useStore();
        const VueRouter = useRouter(); 
        const route$ = useRoute();
    
        const isLoggedIn = ref(VueStore.state.securityStore.isLoggedIn);
            
            async function authenticateIdentity() {
                
                  try {
                    await VueStore.dispatch('securityStore/autoLogin');
                    if (isLoggedIn !== true) {
                      return VueRouter.push({name: "Login", query: {redirect: route$.path}})
                    }
                  } catch (error) {
                    return VueRouter.push({name: "Login", query: {redirect: route$.path}})
                  }
                }
             }
         }

步骤很简单:

  1. 要求VueStore运行action自动登录用户(使用 cookie)
  2. 如果认证通过则什么都不做(即让调用函数继续)
  3. 否则,如果未通过身份验证,则将用户带到Login屏幕

我想将此代码保存在一个单独的文件中,并在我需要的任何组件中使用它。在 Vue3 中如何做到这一点?

不要去

可以创建一个允许在其他地方重用该功能的 Vue3组合。

棘手的部分是vue-router. 我们必须确保可组合内使用的路由器实际上与应用程序/组件内使用的路由器实例相同。我们不能使用useRouter()which 是在组件的设置函数中使用路由器的方法(在我们的可组合中,我们没有设置函数)。因此,我们建议将路由器定义放在一个额外的模块中,并在 App 和可组合定义之间共享该模块。

1.新建文件router.js

import {createRouter, createWebHashHistory} from 'vue-router'

const routes = [
    {path: '/', name: 'Home', component: Home},
    {path: '/login', name: 'Login', component: Login},
    ...
    {path: "/:pathMatch(.*)*", component: NotFound},
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes
})

export default router;

2.使用路由器模块main.js

import {createApp} from 'vue'
import router from './router.js'
import App from './components/App.vue'

const myApp = createApp(App);
myApp.use(router)
     .mount('#app');

3. 创建可组合loggin.js

import {ref} from 'vue';
import store from './store.js';  // assuming this is the file containing the store definition
import router from './router.js';

export async function authenticateIdentity() {
    const isLoggedIn = ref(store.state.securityStore.isLoggedIn);
    try {
        await store.dispatch('securityStore/autoLogin');
        if (isLoggedIn !== true) {
            return router.push({name: "Login", query: {redirect: router.currentRoute.value.path}})
        }
    } catch (error) {
        return router.push({name: "Login", query: {redirect: router.currentRoute.value.path}})
    }
}

4. 在其他组件中使用可组合

<script>
import {authenticateIdentity} from 'loggin.js'

export default {
  name: "LoginComponent",
  methods: {
    login(){
      authenticateIdentity().then( ... ).catch( ... );
    }
  }
}
</script>

(调整项目结构/导入路径/名称)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章