简体   繁体   English

导入 Vuelidate 库的问题

[英]Issues with importing Vuelidate Library

As a part of implementing Form Validation, I want to use Vuelidate library in a Sails.js page.作为实现表单验证的一部分,我想在 Sails.js 页面中使用 Vuelidate 库。 The page gets the Vue code by using this webpage.page.js:该页面通过使用这个webpage.page.js获取Vue代码:

import Vuelidate from 'vuelidate'

parasails.registerPage('web-page', {
    data: {
        message: '',
        class: ''
    },
    validations: {
       // For Vuelidate
       message: {
         required,
       },
       class: {
         required,
       },
    }, 
});

, which is linked to this webpage.ejs View Template: ,它链接到这个网页.ejs 视图模板:

<div id="web-page">
  <div class="container d-flex-column justify-content-center">
        <div class="heading-container">
            <p class="h2">Heading</p>
        </div>
        <form action="/someaction" method="post">
            <div class="form-group" :class="{ 'form-group--error': $v.name.$error }">
                <label 
                      for="name" class="form__label">Name</label>
                <input
                      type="text"
                      class="form__input"
                      name="name"
                      v-model.trim="$v.name.$model"/>
            </div>
           <div class="error" v-if="!$v.name.required">Field is required</div>
           <div class="form-group" :class="{ 'form-group--error': $v.class.$error }">
                <label for="class" class="form__label">Class</label>
                 <input
                      type="text"
                      class="form__input"
                      name="class"
                      v-model.trim="$v.class.$model"/>
           </div>
           <div class="error" v-if="!$v.class.required">Field is required</div>
           <button class="btn btn-success text-white" type="submit">Save</button>
        </form>
    </div>
</div>
<%- /* Expose locals as `window.SAILS_LOCALS` :: */ exposeLocalsToBrowser() %>

This gives an error 'Cannot use import statement outside a module'.这会产生错误“不能在模块外使用导入语句”。 I have the Vuelidate npm package installed.我安装了 Vuelidate npm 包。 Thus, how can I import the Vuelidate library to get this working?因此,如何导入 Vuelidate 库以使其正常工作?

If you are using the Sails web template, you need to import Vuelidate dist files validators.min.js and vuelidate.min.js into the assets/dependencies folder.如果您使用 Sails Web 模板,则需要将 Vuelidate dist 文件validators.min.jsvuelidate.min.js导入到assets/dependencies文件夹中。 After that, Grunt imports the files and you'll be able to use them globally without import because they will be available in the browser.之后,Grunt 导入文件,您无需import就可以全局使用它们,因为它们将在浏览器中可用。

Check the documentation section The browser-ready bundle is also provided in the package.检查文档部分The browser-ready bundle is also provided in the package. :

https://vuelidate.js.org/#sub-basic-usage https://vuelidate.js.org/#sub-basic-usage

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

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