我试图将url中的参数放入vue组件中的方法中.

我已经读到我应该用这个$但无论我做什么,它都返回未定义的结果,我见过一些不同的类似问题,但它们的修复方法对我不起作用.

<template>
  {{ this.$route.query }}
</template>
<script>
  export default {
    name: 'cards',
    data: () => ({
      items: [
        {
          id: 1,
          title: '8 myths about mobile interfaces',
          link: 'https://medium.muz.li/8-myths-about-mobile-interfaces-390d9e2cee33',
          folder: 'medium',
          order: 1,
        },
        {
          id: 2,
          title: 'Asking your user’s gender, the new skeuomorphism, upside-down UIs and more',
          link: 'https://uxdesign.cc/asking-your-users-gender-the-new-skeuomorphism-upside-down-uis-and-more-e108ef888030',
          folder: 'medium',
          order: 2,
        },
      ],
    }),
    methods: {
      link: () => {
        console.log(this.$routes.query);
      },
    },
  };
</script>

我用过这个$在模板中布线,它可以正常工作并显示出来.

我不确定我需要做什么才能让它显示出我的魅力.

我是vue新手,所以可能整个过程都出错了,但基本上应用程序会读取链接,只显示文件夹内容,所以如果它的url是/cards?folder=medium它将仅显示包含文件夹数据的数据.所以如果你知道更好的方法就告诉我!

否则你能明白为什么它没有显示路由吗?

下面是我的路由vue文件,如果有帮助的话:

import Vue from 'vue';
import VueRouter from 'vue-router';

import cards from './cards/cards';

Vue.use(VueRouter);

export default new VueRouter({
  routes: [
    {
      path: '/cards',
      name: 'cards',
      component: cards,
    },
  ],
  mode: 'history',
});

推荐答案

要访问this,不应使用箭头功能.改用常规函数:

methods: {
  links: function() {
    console.log(this.$route) // should work
  }
}

这在Vue文档中有说明,例如这里的https://vuejs.org/v2/api/#data bottom note.

Vue.js相关问答推荐

为什么我的数组在 onBeforeMount 函数中不为空,但在 onMounted 函数中为空

我如何使用vuejs访问彼此嵌套的json对象

传单 vue3 组件变体打开错误的标记 - 单击时弹出

如何解决 importers[path] is not a function for clean Vue 3/Vite/Storybook installation

如何在 v-for 指令切换状态下安全地制作包含附加到对象的复选框的列表(W/O 切换列表中的其他复选框)?

Vue 脚本标签中更漂亮的 destruct 功能

如何从特定索引呈现 v-for

CLI 3 - 如何为 CSS/Sass 创建vendor bundle ?

如何在 vue 3 脚本设置中使用

如何像 React 中的 {...props} 一样在 Vue 中解构 props?

axios.patch / axios.put 在 Vue 和 Laravel 中不起作用

即使在客户端设置 Access-Control-Allow-Origin 或其他 Access-Control-Allow-* 标头后也出现 CORS 错误

在组件外使用 VueI18n 的问题

在Vue.js中从父组件执行子方法

在Vue中单击路由链接上的激活方法

Vue.js webpack:如何获取目录中的文件列表?

vue-router如何持久化导航栏?

使用 axios 和 vue.js 取消先前的请求

vuex - 即使不推荐,是否可以直接更改状态?

如何在 Vue.js 2 应用程序中模拟 onbeforeunload?