如何将 flatpickr 与 vue 组件一起使用?

Gre*_*man 2 javascript vue-component vuejs2

免责声明:我是 vue.js 新手

我需要帮助才能让日期选择器与我的模态组件一起使用。我已经制作了模式并且工作正常,我还安装了一个日期选择器(“flatpickr”),当我在不在组件内的元素上调用它时,它工作得很好。

但是,当我尝试在模态上使用它时,什么也没有发生。我不确定应该在组件中的哪里调用日期选择器,因为我认为您应该避免放置任何具有副作用的内容,因为它们不会被我解析。

我不确定应该在哪里包含 flatpickr 脚本,它应该在模板内还是应该在我的主 app.js 中。

我的模态组件.vue

<template>
<div class="cis-modal is-active">
 <div class="cis-modal-background">
    <div class="cis-modal-content animated bounceInUp">
        <div class="card">
            <div class="card-body">

                <slot></slot>

            </div>
        </div>
    </div>
    <button class="cis-modal-close" @click="$emit('closed')"><i class="fa fa-times" aria-hidden="true"></i></button>
</div>
</div>

<script>
 export default {
 }
</script>
Run Code Online (Sandbox Code Playgroud)

Flatpickr 脚本

<script>
 $("#talentDOB").flatpickr({
 enableTime: true,
 dateFormat: "F, d Y H:i"
});
Run Code Online (Sandbox Code Playgroud)

模态被调用

<modal v-if="showEditTalentModal" @closed="showEditTalentModal = false;">
{!! Form::model($talent, ['method' => 'PATCH', 'action' =>     ['TalentController@update', $talent->user_id], 'files'=>true ]) !!}

<div class="form-group">
  {!! Form::label('dob', 'Date of Birth:') !!}
  {!! Form::text('dob', null, [ 'data-input','class'=>'form-control',  'required' => 'required', 'id' => 'talentDOB']) !!}
</div>

<div class="form-group">
 {!! Form::submit('Update Profile', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</modal>
Run Code Online (Sandbox Code Playgroud)

提前谢谢您。

Kor*_*üpe 5

首先,导入组件。

import flatpickr from 'flatpickr'
Run Code Online (Sandbox Code Playgroud)

然后在mounted中创建flatpicker实例

flatpickr('#datepicker')
Run Code Online (Sandbox Code Playgroud)

当然你需要有一个具有相同id的输入

<input type="text" id="datepicker" v-model="dateValue" autocomplete="off"/>
Run Code Online (Sandbox Code Playgroud)

实际上,在 Vue 中使用它没有任何特殊要求。

  • 我还必须手动加载 css 才能使其工作 `require("flatpickr/dist/flatpickr.css")` (4认同)