简体   繁体   English

为什么在课堂上不触发 JS 警报消息

[英]Why JS alert message is not fired when in class

I am learning wordpress plugin development.我正在学习 wordpress 插件开发。 I have dummy question - Why, the alert message is not fired, when located in js class or in index.js?我有一个假问题 - 为什么,当位于 js 类或 index.js 中时,不会触发警报消息?

I created new file "test.php" in my plugin folder我在插件文件夹中创建了新文件“test.php”

<?php
/*
Plugin Name: Test
Description: test plugin
Version: 1.0
Author: Jarda

*/


class Test{

    function __construct(){
add_action('wp_enqueue_scripts', array($this, 'startMyScripts'));
    }

    function startMyScripts(){
        wp_enqueue_script( 'testScript', plugin_dir_url( __FILE__ ) . '/src/index.js' );
    }
    
    $test = new Test();
}

Than I created js file "index.js" in /src/比我在 /src/ 中创建 js 文件“index.js”

import Search from "./modules/Search"
const search = new Search()

And a class "Search" in /src/modules和 /src/modules 中的“搜索”类

    class Search{
    constructor(){
    alert("helo")
    console.log("fired")
    }
}

export default Search

I have node installed, package.json file looks like this我已经安装了节点,package.json 文件看起来像这样

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "wp-scripts build",
    "start": "wp-scripts start",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@wordpress/scripts": "^23.5.0"
  }
}

好的,所以路径必须是 wp_enqueue_script( 'testScript', plugin_dir_url( FILE ) . '/build/index.js' );

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

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