在将Vue.js与Typescript一起使用时,如何使用vue

编程入门 行业动态 更新时间:2024-10-27 14:28:53
本文介绍了在将Vue.js与Typescript一起使用时,如何使用vue-resource等插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开始使用Typescript并尝试将其应用于我的项目。但是,我不能让Vue.js插件像vue-resource一样使用它。

I started using Typescript and trying to apply it to my project. However, I can't get Vue.js plugins like vue-resource to work with it.

当我使用

this.$http.post()

I得到错误:

错误TS2339:类型'typeof Vue'上不存在属性'$ http'。

error TS2339: Property '$http' does not exist on type 'typeof Vue'.

这是有道理的,因为我在一个类上下文中。但是我该怎么做呢?这是我的完整组成部分:

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>

我在 main.ts 像这样:

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 。

import VueResource from 'vue-resource'

更多推荐

在将Vue.js与Typescript一起使用时,如何使用vue

本文发布于:2023-11-24 19:26:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626506.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   js   Vue   vue   Typescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!