[英]Vue with Typescript - using components without definition
I'm trying to use vue-instant component as a child component. 我正在尝试使用vue即时组件作为子组件。 I'm not sure how to add components without definition, or maybe my issue is in the webpack config ignoring node_modules because of the lack of type?
我不确定如何添加未定义的组件,或者我的问题是由于缺少类型而忽略了node_modules的webpack配置? Here's what I have:
这是我所拥有的:
SingleUserSearch.vue (my custom component): SingleUserSearch.vue(我的自定义组件):
<template>
<div class="input-group">
<vue-instant
v-model="value"
@input="changed"
:suggestions="suggestions"
name="customName"
placeholder="custom placeholder"
type="google"></vue-instant>
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import axios from "axios";
import VueInstant from 'vue-instant'
let vueInstantComponent : Vue.Component = VueInstant;
@Component({
components: {
'vue-instant': vueInstantComponent
}
})
export default class SingleUserSearch extends Vue {
value:string="";
suggestions : Array<string> = []
async changed() {
var that = this
this.suggestions = []
let response = await axios.get('https://api.themoviedb.org/3/search/movie?api_key=342d3061b70d2747a1e159ae9a7e9a36&query=' + this.value);
alert(response);
}
}
</script>
Then I compile my code using webpack without difficulties. 然后,我可以毫无困难地使用webpack编译代码。 When I try to test the code on page I get:
当我尝试测试页面上的代码时,我得到:
[Vue warn]: Failed to mount component: template or render function not defined.
[Vue警告]:无法安装组件:未定义模板或渲染功能。
found in
在发现
---> at scripts\\vue\\components\\SingleUserSearch.vue
--->在scripts \\ vue \\ components \\ SingleUserSearch.vue
webpack.config.js webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './scripts/vue/main.ts',
output: {
path: path.resolve('./scripts/build/'),
filename: 'app.js'
},
module: {
loaders: [
{
test: /\.tsx?$/,
loader: 'ts-loader?' + JSON.stringify({
transpileOnly: true
})
},
{
test: /\.vue$/,
loader:'vue-loader'
},
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015','stage-0','stage-1','stage-2','stage-3']
}
}
]
},
resolve: {
extensions: ['.js', '.vue'],
alias: {
'vue': path.resolve('./node_modules/vue/dist/vue.esm.js')
}
},
stats: {
colors: true
},
devtool: 'source-map'
};
The issue isn't that they types are missing. 问题不是它们缺少类型。 Unless you're using TypeScript in strict mode, anything without types is imported as
any
. 除非您在严格模式下使用TypeScript,否则所有不带类型的内容都将作为
any
导入。
The issue is that VueImport's default export is a plugin object, so when you import VueInstant from 'vue-instant'
you're not actually importing the component. 问题在于VueImport的默认导出是一个插件对象,因此,当您
import VueInstant from 'vue-instant'
实际上并没有导入该组件。
Just console.log(VueInstant)
and you'll see what I mean. 只需
console.log(VueInstant)
,您就会明白我的意思。
Instead of using the default import, you'll need to either specify what you would like to import by using ES6 destructuring: 除了使用默认导入,您还需要通过使用ES6解构来指定要导入的内容:
import { VueInstant } from 'vue-instant'
VueInstant
Alternatively you can import all the exported modules under an alias, and then call the class from there: 或者,您可以在别名下导入所有导出的模块,然后从那里调用类:
import * as VueInstant from 'vue-instant'
VueInstant.VueInstant
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.