我正在使用 Vue 3,包括 Composition API,另外还使用 Pinia 作为状态管理。
在选项 API 中,有一个方法 beforeRouteEnter,该方法内置于组件本身中。不幸的是,这个方法在组合 API 中不存在。这里,本来应该在 beforeRouteEnter 方法中的代码被直接写入 setup 方法中。但是,这意味着首先加载并显示组件,然后执行代码,如果检查失败,则组件将被重定向到错误页面等。
我的想法是直接在路由的 beforeEnter 方法中的路由配置中进行检查。但是,我无法访问 Pinia Store,尽管它之前在 main.js 中调用过,但它似乎尚未初始化。
控制台日志
Uncaught Error: []: getActivePinia was called with no active Pinia. Did you forget to install pinia?
const pinia = createPinia()
app.use(pinia)
This will fail in production.
Run Code Online (Sandbox Code Playgroud)
路由器.js
import { useProcessStore } from "@/store/process";
const routes: Array<RouteRecordRaw> = [
{
path: "/processes/:id",
name: "ProcessView",
component: loadView("ProcessView", "processes/"),
beforeEnter: () => {
const processStore = useProcessStore();
console.log(processStore); …Run Code Online (Sandbox Code Playgroud) 我有以下package.json文件:
{
"name": "view",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"@vue-leaflet/vue-leaflet": "^0.6.1",
"autoprefixer": "^10.3.1",
"core-js": "^3.6.5",
"gpxparser": "^3.0.8",
"leaflet": "^1.7.1",
"postcss": "^8.3.6",
"tailwindcss": "^2.2.7",
"typescript": "^4.3.5",
"vue": "^3.0.0",
"vue-router": "^4.0.0-0",
"vue3-openlayers": "^0.1.38",
"vuex": "^4.0.2"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^5.0.0-beta.3",
"@vue/cli-plugin-eslint": "^5.0.0-beta.3",
"@vue/cli-plugin-router": "^5.0.0-beta.3",
"@vue/cli-service": "^5.0.0-beta.3",
"@vue/compiler-sfc": "^3.0.0",
"@vue/eslint-config-prettier": "^6.0.0",
"babel-eslint": "^10.1.0",
"eslint": "^7.32.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^7.0.0",
"prettier": "^2.2.1",
"sass": "^1.26.5",
"sass-loader": "^8.0.2"
}
}
Run Code Online (Sandbox Code Playgroud)
如何将打字稿添加到这个 …
我在 vue 中定义了一个路由:
/users/:userId
Run Code Online (Sandbox Code Playgroud)
哪个指向 UserComponent:
<template>
<div>{{username}}</div>
</template>
Run Code Online (Sandbox Code Playgroud)
我使用computedfrom@vue/composition-api来获取数据。
问题是当路由更改为另一个时userId,通过导航到另一个用户,html 模板中的用户没有像我预期的那样更改。当用户不在列表中时,它也不会重定向。
那么我能做些什么来解决这个问题?
这是我的代码:
<template>
<div>{{username}}</div>
</template>
<script lang="ts">
import { computed, defineComponent, ref, getCurrentInstance } from '@vue/composition-api';
export const useUsername = ({ user }) => {
return { username: user.name };
};
export default defineComponent({
setup(props, { root }) {
const vm = getCurrentInstance();
const userToLoad = computed(() => root.$route.params.userId);
const listOfUsers = [
{ userId: 1, name: 'user1' },
{ userId: 2, …Run Code Online (Sandbox Code Playgroud) Vue v3文档展示了一个通过将数组作为参数传递来使用单个观察者观察多个引用的示例
我可以(以及如何)使用类似的方法通过单个观察者同时观察ref和对象?reactive
const state = reactive({
postPerPage: 10,
currentPage: 1,
});
const posts = ref([]);
const currentPosts = ref([]);
watch(
() => posts.value, // store value in currentPosts when the posts.value is updated (get data from api)
() => {
currentPosts.value = getCurrentPost();
}
)
watch(
() => {
return { ...state };
}
() => {
currentPosts.value = getCurrentPost();
}
)
Run Code Online (Sandbox Code Playgroud) 我在 TS 中使用 Vue3(最后一个 vue-cli)。
我想在 vue-loader 编译 .vue 文件时获取所有节点(vnodes)元素。我需要读取节点属性并删除所有“数据测试”。
我尝试在 vue.config.js 中使用:
module.exports = {
chainWebpack: (config) => {
config.module
.rule('vue')
.use('vue-loader')
// .loader('vue-loader') // same with
.tap((options) => {
options.compilerOptions = {
...(options.compilerOptions || {}),
modules: [ // never enter here
{
preTransformNode(node) {
// if (process.env.NODE_ENV === 'production') {
const { attrsMap, attrsList } = node
console.log(node)
if (attrsMap['qa-id']) {
delete attrsMap['qa-id']
const index = attrsList.findIndex(
(x) => x.name === 'data-test'
)
attrsList.splice(index, 1)
}
// } …Run Code Online (Sandbox Code Playgroud) 我正在使用 typescript 为 vite、vue 和 vuetify 开发一个简单的脚手架,我想使用 SFC vue 的脚本设置版本
<script setup lang="ts">
Run Code Online (Sandbox Code Playgroud)
我不明白的一件事是如何访问“this”关键字属性?
例如在我的旧 vue 项目中我可以这样写
this.$vuetify.themes.light.colors.primary
Run Code Online (Sandbox Code Playgroud)
所以我能够在组件中的任何位置访问 $vuetify,但现在在脚本设置模式下“this”关键字未定义;如何访问“this”属性?
我在 Vue 路由器中的路线:
{ path: 'articles/create', component: () => import('Detail.vue') },
{ path: 'articles/:id/edit', component: () => import('Detail.vue') },
Run Code Online (Sandbox Code Playgroud)
如您所见,我Detail.vue在两条路线上渲染相同的 Vue 组件。
当 URL从例如变为时,如何“强制”Vue 销毁并重新创建Detail.vue组件?/articles/5/edit/articles/create
我过去已经这样做过很多次了,我在 netlify 日志中收到此错误。我尝试部署的存储库是https://github.com/yanyamz/Key_Visual_Arts。
\n我检查了它是否在 Netlify 的大小限制内,结果是,总共 15mb 左右,而您可以在 netlify 上部署的 20+mb 左右。
\n12:03:03 PM: Build ready to start\n12:03:04 PM: build-image version: fa439ad1ab9393b2c0d449d8d7c033927683f4b0\n12:03:04 PM: build-image tag: v4.3.0\n12:03:04 PM: buildbot version: 0f2f658d862cfe72bae7cc05c6a8de0426a5a0e2\n12:03:04 PM: Fetching cached dependencies\n12:03:05 PM: Failed to fetch cache, continuing with build\n12:03:05 PM: Starting to prepare the repo for build\n12:03:05 PM: No cached dependencies found. Cloning fresh repo\n12:03:05 PM: git clone https://github.com/yanyamz/Key_Visual_Arts\n12:03:07 PM: Preparing Git Reference refs/heads/main\n12:03:07 PM: Parsing package.json dependencies\n12:03:08 PM: \n\xe2\x80\x8b\n\xe2\x9d\xaf Initial build environment\nbaseRelDir: …Run Code Online (Sandbox Code Playgroud) 我对 Vue 非常陌生,所以如果我的术语不正确,请原谅我。我有一个 .NET Core MVC 项目,其中包含小的、单独的 vue 页面。在我当前的页面上,我从控制器返回一个视图,它只有:
@model long;
<div id="faq-category" v-bind:faqCategoryId="@Model"></div>
@section Scripts {
<script src="~/scripts/js/faqCategory.js"></script>
}
Run Code Online (Sandbox Code Playgroud)
我在此页面中发送项目的 id 的地方将抓取并为其创建编辑表单。 faqCategory.js是编译后的 vue 应用程序。我需要long在初始化时将参数传递给 vue 应用程序,以便它可以获取完整的对象。我用main.ts类似的方式安装它:
import { createApp } from 'vue'
import FaqCategoryPage from './FaqCategoryPage.vue'
createApp(FaqCategoryPage)
.mount('#faq-category');
Run Code Online (Sandbox Code Playgroud)
如何faqCategoryId进入我的 vue 应用程序以启动初始化并加载对象?我的v-bind尝试似乎不起作用 - 我@Prop(Number) readonly faqCategoryId: number = 0;在 vue 组件上有一个,但它总是0.
我的 FaqCategoryPage.vue 脚本很简单:
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { Prop …Run Code Online (Sandbox Code Playgroud) vue.js ×10
vuejs3 ×7
typescript ×2
components ×1
deployment ×1
javascript ×1
netlify ×1
pinia ×1
quasar ×1
render ×1
rerender ×1
vue-cli ×1
vue-loader ×1
vue-router ×1
vuejs2 ×1
vuetify.js ×1
watch ×1