我正在嘗試通過或多或少默認安裝 Symfony6 和 Encore 來學習 Stimulus,與默認安裝的主要區別之一是我使用的是 Typescript。Stimulus 檔案說應該像這樣使用型別值= >
export default class extends Controller {
static values = { index: Number }
initialize() {
console.log(this.indexValue)
console.log(typeof this.indexValue)
}
// …
}
但是,當使用 Typescript 時,我得到一個 TS2339: Property 'indexValue' does not exist on type 'default'.on access this.indexValue;我設法擺脫了這個,@ts-ignore但我認為這很糟糕。你知道在 webpack Encore Symfony 專案中有什么更簡潔的方法來處理這個問題嗎?提前謝謝??
我的刺激控制器 =>
import { Controller } from '@hotwired/stimulus';
default class extends Controller {
static values = {
index: Number
}
doSomethingAsync(event: Event) {
event.preventDefault();
// @ts-ignore
console.log("indexValue", this.indexValue);
}
}
我的 tsconfig =>
{
"compilerOptions": {
"sourceMap": true,
"noImplicitAny": true,
"module": "es6",
"target": "es6",
"jsx": "react",
"allowJs": true,
"moduleResolution": "node"
},
"exclude": [
"node_modules"
]
}
我的webpack.config.js=>
const Encore = require('@symfony/webpack-encore');
// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if (!Encore.isRuntimeEnvironmentConfigured()) {
Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}
Encore
// directory where compiled assets will be stored
.setOutputPath('public/build/')
// public path used by the web server to access the output path
.setPublicPath('/build')
// only needed for CDN's or sub-directory deploy
//.setManifestKeyPrefix('build/')
/*
* ENTRY CONFIG
*
* Each entry will result in one JavaScript file (e.g. app.ts)
* and one CSS file (e.g. app.css) if your JavaScript imports CSS.
*/
.addEntry('app', './assets/app.ts')
// enables the Symfony UX Stimulus bridge (used in assets/bootstrap.ts)
.enableStimulusBridge('./assets/controllers.json')
// When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
.splitEntryChunks()
// will require an extra script tag for runtime.js
// but, you probably want this, unless you're building a single-page app
.enableSingleRuntimeChunk()
/*
* FEATURE CONFIG
*
* Enable & configure other features below. For a full
* list of features, see:
* https://symfony.com/doc/current/frontend.html#adding-more-features
*/
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
// enables hashed filenames (e.g. app.abc123.css)
.enableVersioning(Encore.isProduction())
.configureBabel((config) => {
config.plugins.push('@babel/plugin-proposal-class-properties');
})
// enables @babel/preset-env polyfills
.configureBabelPresetEnv((config) => {
config.useBuiltIns = 'usage';
config.corejs = 3;
})
// enables Sass/SCSS support
.enableSassLoader()
// uncomment if you use TypeScript
.enableTypeScriptLoader()
// uncomment if you use React
//.enableReactPreset()
// uncomment to get integrity="..." attributes on your script & link tags
// requires WebpackEncoreBundle 1.4 or higher
//.enableIntegrityHashes(Encore.isProduction())
// uncomment if you're having problems with a jQuery plugin
//.autoProvidejQuery()
;
module.exports = Encore.getWebpackConfig();
我的package.json=>
{
"devDependencies": {
"@fontsource/roboto-condensed": "^4.5.8",
"@fortawesome/fontawesome-free": "^6.1.1",
"@hotwired/stimulus": "^3.0.0",
"@symfony/stimulus-bridge": "^3.0.0",
"@symfony/stimulus-bridge-types": "^1.0.0",
"@symfony/webpack-encore": "^2.0.0",
"axios": "^0.27.2",
"bootstrap": "^5.1.3",
"core-js": "^3.0.0",
"regenerator-runtime": "^0.13.2",
"sass": "^1.52.1",
"sass-loader": "^12.0.0",
"ts-loader": "^9.0.0",
"typescript": "^4.7.2",
"webpack-notifier": "^1.6.0"
},
"license": "UNLICENSED",
"private": true,
"scripts": {
"dev-server": "encore dev-server",
"dev": "encore dev",
"watch": "encore dev --watch",
"build": "encore production --progress"
},
"browserslist": [
"defaults",
"not IE 11"
]
}
uj5u.com熱心網友回復:
Stimulus 中的 Typescript 支持仍然有一些粗糙的邊緣,但是這里有一個許多用戶發現適合他們的解決方案。
首先設定一個包含通用型別和忽略注釋的抽象類。
import { Controller } from '@hotwired/stimulus';
export default abstract class AbstractController<StimulusElement extends Element> extends Controller {
// @ts-ignore see https://discuss.hotwired.dev/t/stimulus-and-typescript/2458
declare readonly element: StimulusElement;
}
然后在你的控制器中,擴展抽象的,有些事情會更容易,你仍然需要宣告單獨使用indexValue.
import AbstractController from '../src/AbstractController';
export default class extends AbstractController<HTMLButtonElement> {
static values = {
index: Number,
};
static targets = [
'name'
];
declare readonly indexValue: number
declare readonly nameTarget: HTMLInputElement
}
https://discuss.hotwired.dev/t/stimulus-and-typescript/2458/2
附加鏈接
- PR 在未來版本中帶來更好的 Typescript 支持https://github.com/hotwired/stimulus/pull/529
- PR 與有關使用 Typescript 為未來版本的檔案https://github.com/hotwired/stimulus/pull/540/files
- 與 Typescript 有關的問題https://github.com/hotwired/stimulus/issues/457
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/491699.html
標籤:打字稿 交响乐 网页包 webpack-encore 刺激
下一篇:雖然,不在Python中回圈
