[英]Why JS alert message is not fired when in class
我正在學習 wordpress 插件開發。 我有一個假問題 - 為什么,當位於 js 類或 index.js 中時,不會觸發警報消息?
我在插件文件夾中創建了新文件“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();
}
比我在 /src/ 中創建 js 文件“index.js”
import Search from "./modules/Search"
const search = new Search()
和 /src/modules 中的“搜索”類
class Search{
constructor(){
alert("helo")
console.log("fired")
}
}
export default Search
我已經安裝了節點,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.