我的問題是,當我使用 setup 時,我無法在普通腳本中訪問它,但只有在我以 Dev 模式構建應用程式之后,一切正常。在此示例中,我無法在我的注銷功能中訪問 this.authStore。
`
<script setup>
import { ref } from "vue";
import { storeToRefs } from "pinia";
import { useAuthStore } from "@/stores/auth-store";
const authStore = useAuthStore();
const { user } = storeToRefs(authStore);
const searchDropdown = ref(false);
const showSearchDropdown = () => {
searchDropdown.value = true;
};
const hideSearchDropdown = () => {
searchDropdown.value = false;
};
</script>
<script>
export default {
name: "Top Bar",
data() {
return {
pageName: this.$route.name,
};
},
methods: {
async logout() {
await this.authStore.logout();
},
},
};
</script>
`
uj5u.com熱心網友回復:
嘗試僅使用組合 API 使您的代碼保持一致,將logout方法替換為函式,用于useRoute獲取路由名稱:
<script setup>
import { ref } from "vue";
import { usRoute } from 'vue-router'
import { storeToRefs } from "pinia";
import { useAuthStore } from "@/stores/auth-store";
const authStore = useAuthStore();
const { user } = storeToRefs(authStore);
const searchDropdown = ref(false);
const route = useRoute()
const pageName=route.name
const showSearchDropdown = () => {
searchDropdown.value = true;
};
const hideSearchDropdown = () => {
searchDropdown.value = false;
};
function logout(){
authStore.logout();
}
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/526472.html
