简体   繁体   English

尝试使用 axios 进行 VueJS API 调用以在名为 prijzen 的组件中获取 json

[英]Trying to make a VueJS API call with axios to get json in a component named prijzen

I am new to Vue and am trying to load in json data from a REST API called JSON placeholder.我是 Vue 的新手,正在尝试从名为 JSON 占位符的 REST API 加载 json 数据。 I get the error:我收到错误:

"Uncaught ReferenceError: Prijzen is not defined at eval (cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/views/layout/Prijzen.vue?vue&type=script&lang=js&:16) at Module../node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/views/layout/Prijzen.vue?" “未捕获的 ReferenceError: Prijzen 未在 eval (cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue -loader/lib/index.js?!./src/views/layout/Prijzen.vue?vue&type=script&lang=js&:16) 在 Module../node_modules/cache-loader/dist/cjs.js?!./ node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/views/layout/ Prijzen.vue?”

So after i got this i tried looking at index.js but prijzen is added there.. the code looks like this:所以在我得到这个之后,我尝试查看 index.js 但在那里添加了 prijzen .. 代码如下所示:

import Vue from 'vue'
import Router from 'vue-router'

// Containers
const TheContainer = () => import('@/containers/TheContainer')

// Views
const Dashboard = () => import('@/views/Dashboard')
const Prijzen = () => import('@/views/layout/Prijzen')
const Login = () => import('@/views/layout/Login')

Vue.use(Router)

export default new Router({
  mode: 'hash', // https://router.vuejs.org/api/#mode
  linkActiveClass: 'open active',
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    {
      path: '/',
      redirect: '/dashboard',
      name: 'Home',
      component: TheContainer,
      children: [
        {
            path: 'dashboard',
            name: 'Dashboard',
            component: Dashboard
        },
        {
          path: '/login',
          name: 'Login',
          component: Login
        },
        {
          path: '/layout/prijzen',
          name: 'Prijzen',
          component: Prijzen
      }
      ]
    },
  ]
})

So prijzen is fine here.所以prijzen在这里很好。 At least it looks fine to me.至少在我看来还不错。 This is how the prijzen component looks:这是 prijzen 组件的外观:

<template>
  <div id="Prijzen">
    {{ prijs }}
  </div>
</template>

<script>
import axios from 'axios';
import PrijsItem from './PrijsItem.vue';

export default {
  name: 'Prijzen',
  components: {
    PrijsItem
  },
  props: ["PrijsItem"]
}
</script>

I have used {{ prijs }} in the template and in my main.js file it is being mentioned with my axios get request as you can see here:我在模板和我的 main.js 文件中使用了 {{ prijs }} ,我的 axios get 请求提到了它,你可以在这里看到:

import 'core-js/stable'
import Vue from 'vue'
import CoreuiVue from '@coreui/vue/src'
import App from './App'
import router from './router/index'
import { iconsSet as icons } from './assets/icons/icons.js'
import store from './store'

Vue.use(CoreuiVue)

new Vue({
  el: '#app',
  router,
  store,
  //CIcon component documentation: https://coreui.io/vue/docs/components/icon
  icons,
  template: '<App/>',
  components: {
    App,
    Prijzen
  },

  new:({
    let: '#Prijzen',
    data () {
      return {
        prijs: null
      }
    },
    mounted () {
      axios
        .get('https://jsonplaceholder.typicode.com/posts')
        .then(response => (this.prijs = response))
    }
  })
})

Is the Prijzen.vue file definitely in the views/layout directory? Prijzen.vue 文件肯定在views/layout 目录下吗? That error looks like it might be caused by not being able to find the file where you've told it to look.该错误看起来可能是由于无法找到您告诉它查看的文件造成的。

A different issue:一个不同的问题:

{{ prijs }}

Is in the template, but prijs is not defined.在模板中,但未定义 prijs。 Also PrijsItem component has been imported but not used which will sometimes throw linting errors. PrijsItem 组件也已导入但未使用,这有时会引发 linting 错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM