JS引用外部JS的问题。
环境是VUE3 + ts。我在main.ts中登录腾讯云,代码是这样的:
import { createApp } from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import cloudbase from "@cloudbase/js-sdk";
const app: cloudbase.app.App = cloudbase.init({
env: "hel96c"
});
const auth: cloudbase.auth.App = app.auth({
persistence: "session"
});
(async function login() {
await auth.anonymousAuthProvider().signIn();
const loginState: cloudbase.auth.ILoginState | null = await auth.getLoginState();
console.log('登录状态:', loginState?.isAnonymousAuth);
}())
createApp(App).use(router).mount('#app')
功能已实现。
但现在我想将这个登录抽离出来,做import。
我尝试这样:
main.ts
import { createApp } from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import { login } from '../src/common/js/loginCloudbase'
const loginStatusCloudbase = login
console.log(loginStatusCloudbase)
createApp(App).use(router).mount('#app')
loginCloudbase.ts
import cloudbase from "@cloudbase/js-sdk";
export const login = async function () {
const app: cloudbase.app.App = cloudbase.init({
env: "hel96c"
});
const auth: cloudbase.auth.App = app.auth({
persistence: "session"
});
await auth.anonymousAuthProvider().signIn();
const loginState: cloudbase.auth.ILoginState | null = await auth.getLoginState();
console.log(loginState?.isAnonymousAuth);
return loginState?.isAnonymousAuth
}
但发现这样只能将函数本身赋值到main.ts的loginStatusCloudbase,并不是执行结果。
麻烦大佬帮我看看应该怎么改,帮忙改一下,感谢。 import { login } from '../src/common/js/loginCloudbase'
const loginStatusCloudbase = login()
console.log(loginStatusCloudbase)
页:
[1]