Vue.js export default not working
我正在跟踪表单示例,但它不起作用::
https://bootstrap-vue.js.org/docs/components/form
这是我的代码,位于我简单的基本index.html中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css"/> <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css"/> <script src="https://unpkg.com/vue"> <script src="https://unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"> <script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"> </head> <body> <template> <b-form @submit="onSubmit" @reset="onReset" v-if="show"> <b-form-group id="exampleInputGroup1" label="Email address:" label-for="exampleInput1" description="We'll never share your email with anyone else."> <b-form-input id="exampleInput1" type="email" v-model="form.email" required placeholder="Enter email"> </b-form-input> </b-form-group> <b-form-group id="exampleInputGroup2" label="Your Name:" label-for="exampleInput2"> <b-form-input id="exampleInput2" type="text" v-model="form.name" required placeholder="Enter name"> </b-form-input> </b-form-group> <b-form-group id="exampleInputGroup3" label="Food:" label-for="exampleInput3"> <b-form-select id="exampleInput3" :options="foods" required v-model="form.food"> </b-form-select> </b-form-group> <b-form-group id="exampleGroup4"> <b-form-checkbox-group v-model="form.checked" id="exampleChecks"> <b-form-checkbox value="me">Check me out</b-form-checkbox> <b-form-checkbox value="that">Check that out</b-form-checkbox> </b-form-checkbox-group> </b-form-group> <b-button type="submit" variant="primary">Submit</b-button> <b-button type="reset" variant="danger">Reset</b-button> </b-form> </template> </body> |
和js代码1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | export default { data () { return { form: { email: '', name: '', food: null, checked: [] }, foods: [ { text: 'Select One', value: null }, 'Carrots', 'Beans', 'Tomatoes', 'Corn' ], show: true } }, methods: { onSubmit (evt) { evt.preventDefault(); alert(JSON.stringify(this.form)); }, onReset (evt) { evt.preventDefault(); /* Reset our form values */ this.form.email = ''; this.form.name = ''; this.form.food = null; this.form.checked = []; /* Trick to reset/clear native browser form validation state */ this.show = false; this.$nextTick(() => { this.show = true }); } } } |
它与vue bootstrap网站上的内容完全相同,并且不起作用
有一个空白屏幕,这是Firefox错误:
SyntaxError:导出声明??只能出现在模块的顶层
我无法显示我的表格,什么也没有,只有一个空白屏幕,它不起作用!
注意:我不想使用babel或任何复杂的东西,我只需要我的index.html即可工作
请帮助我谢谢您
您逐字复制的示例是Vue单个文件组件的内容。这些需要将Webpack或Browserify转换为浏览器可以理解的实际JavaScript。
如果要获得可重用的Vue组件,可以使用
否则,如果只有一页,则只需使用
1 2 3 4 5 6 | var app = new Vue({ el: '#app', data: { ... } }) |
并在您的html中:
1 2 3 | ... ... vue perform rendering and interpolation here ... ... |
您在简单的HTML文件中将单文件组件与Vue混淆了。
要获得后者,您可以如下所示。
基本上,您必须:
-
删除
<template> 标记,并在其内部div 中添加一个id ,例如: -
包装在Vue构造函数中导出的对象,并为其添加
el 选项:1
2
3new Vue({
el: '#app',
data() {
JSBin演示在这里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css"/> <link type="text/css" rel="stylesheet" href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css"/> <script src="https://unpkg.com/vue"> <script src="https://unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"> <script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"> </head> <body> <b-form @submit="onSubmit" @reset="onReset" v-if="show"> <b-form-group id="exampleInputGroup1" label="Email address:" label-for="exampleInput1" description="We'll never share your email with anyone else."> <b-form-input id="exampleInput1" type="email" v-model="form.email" required placeholder="Enter email"> </b-form-input> </b-form-group> <b-form-group id="exampleInputGroup2" label="Your Name:" label-for="exampleInput2"> <b-form-input id="exampleInput2" type="text" v-model="form.name" required placeholder="Enter name"> </b-form-input> </b-form-group> <b-form-group id="exampleInputGroup3" label="Food:" label-for="exampleInput3"> <b-form-select id="exampleInput3" :options="foods" required v-model="form.food"> </b-form-select> </b-form-group> <b-form-group id="exampleGroup4"> <b-form-checkbox-group v-model="form.checked" id="exampleChecks"> <b-form-checkbox value="me">Check me out</b-form-checkbox> <b-form-checkbox value="that">Check that out</b-form-checkbox> </b-form-checkbox-group> </b-form-group> <b-button type="submit" variant="primary">Submit</b-button> <b-button type="reset" variant="danger">Reset</b-button> </b-form> new Vue({ el: '#app', data() { return { form: { email: '', name: '', food: null, checked: [] }, foods: [ {text: 'Select One', value: null}, 'Carrots', 'Beans', 'Tomatoes', 'Corn' ], show: true } }, methods: { onSubmit(evt) { evt.preventDefault(); alert(JSON.stringify(this.form)); }, onReset(evt) { evt.preventDefault(); /* Reset our form values */ this.form.email = ''; this.form.name = ''; this.form.food = null; this.form.checked = []; /* Trick to reset/clear native browser form validation state */ this.show = false; this.$nextTick(() => { this.show = true }); } } }); </body> </html> |
上面的代码直接在Vue实例中使用您的代码。
如果要将表单用作组件,例如:
1 | <my-form></my-form> |
关注此JSBin演示。