小编Vla*_*lad的帖子

如何捕获Vue.js上自定义指令的click事件?

我正在尝试学习Vue.js,并来到一个练习示例,在该示例中,我需要实现一个自定义指令,使虱子“ v-on”工作。这意味着我需要捕获自定义指令上的click事件并调用一个方法。

我在想的模板。

<template>
    <h1 v-my-on:click="alertMe">Click</h1>
</template>
Run Code Online (Sandbox Code Playgroud)

问题是我不知道如何在自定义指令中捕获click事件。请原谅下面的笨拙代码。

<script>
    export default {
        methods: {
            alertMe() {
                alert('The Alert!');
            }
        },
        directives: {
            'my-on': {
                bind(el, binding, vnode) {
                    console.log('bind');

                    el.addEventListener('click',()=>{
                        console.log('bind');
                        vnode.context.$emit('click');
                    });
                },

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

谁能帮助我了解其运作方式?我没有找到类似例子的任何例子。

vue.js vuejs2 vue.js-directives

3
推荐指数
1
解决办法
4306
查看次数

我如何在 Laravel 中使用 faker 随机生成一个 id 或 null?

我正在尝试使用同一个表的随机 id 为 parent_id 列设置种子,或者让它为空。

这我认为它会起作用:

...
'parent_id' => $faker->boolean() ? Page::all()->random()->id : null,
...
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

  You requested 1 items, but there are only 0 items available. 
Run Code Online (Sandbox Code Playgroud)

有谁知道如何做到这一点?

更新1:

使用伪动漫答案我尝试了流动:

$factory->define(Page::class, function (Faker\Generator $faker) {
...
    $parent_id = null;
...
    $has_parent = $faker->boolean(50);
    Log::debug('$has_parent' . $has_parent);
    if ($has_parent) {
        $parents = Page::all();
        Log::debug('$parents' . count($parents));

        if ($parents->isEmpty()) {
            Log::debug('isEmpty');

            $parent_id = null;
        } else {
            Log::debug('$parents->random()' . print_r($parents->random(), true));
            $parent_id = $parents->random()->id;
        }
    }

    return [
...

        'parent_id' => $parent_id, …
Run Code Online (Sandbox Code Playgroud)

seeding faker laravel laravel-5

-1
推荐指数
3
解决办法
8910
查看次数

标签 统计

faker ×1

laravel ×1

laravel-5 ×1

seeding ×1

vue.js ×1

vue.js-directives ×1

vuejs2 ×1