简体   繁体   English

将Vue.js与Typescript一起使用时如何使用vue-resource之类的插件?

[英]How to use plugins like vue-resource when using Vue.js with Typescript?

I started using Typescript and trying to apply it to my project. 我开始使用Typescript并尝试将其应用于我的项目。 However, I can't get Vue.js plugins like vue-resource to work with it. 但是,我无法像vue-resource那样使用Vue.js插件。

When I use 当我使用

this.$http.post()

I get the error: 我得到错误:

error TS2339: Property '$http' does not exist on type 'typeof Vue'. 错误TS2339:类型'typeof Vue'上不存在属性'$ http'。

which makes sense because I am in a class context. 这很有意义,因为我处于课堂环境中。 But how can I do that? 但是我该怎么办呢? This is my full component: 这是我的全部内容:

<template>
<div>
  <h1>Sign up</h1>

  <form>
    <div class="form-group">
      <label for="name">Name</label>
      <input v-model="name" type="text" class="form-control" name="name" placeholder="Name">
      <small class="form-text text-muted">Please provide a name.</small>
    </div>
    <div class="form-group">
      <label for="name">Password</label>
      <input v-model="password" type="password" class="form-control" name="password" placeholder="Password">
      <small class="form-text text-muted">Please provide a password.</small>
    </div>
    <input type="submit" class="btn btn-primary" value="Submit" @click.prevent="save">
  </form>
</div>
</template>

<script lang="ts">
import Component from 'vue-class-component'

@Component
export default class SignUp extends Vue {
  name: string = ''
  password: string = ''

  save(): void {
    this.$http.post('/api/sign-up', {
        name: this.name,
        password: this.password
      })
      .then((response: any) => {
        console.log(response)
      })
  }
}
</script>

And I register vue-resource in my main.ts like this: 然后像这样在我的main.ts注册vue-resource:

import Vue from "vue"
import router from "./router"
import App from "./app"

const VueResource = require('vue-resource')

Vue.use(VueResource)

new Vue({
  el: "#app",
  router,
  template: "<App/>",
  components: { App },
});

采用import的,而不是require对VueResource了。

import VueResource from 'vue-resource'

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

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