Materialise css 旋转木马在 Angular 中不起作用
Materialize css carousel doesn't work in Angular
我是 Angular 的新手,我遇到了问题。我成功地将 materialize 导入 Angular,materialize css 元素有效,但 js 元素无效。轮播和其他元素(例如 select)没有出现在我的组件中。只是一个空位。
这里是package.json
"private": true,
"dependencies": {
"@angular/animations": "^10.0.14",
"@angular/common": "~10.0.11",
"@angular/compiler": "~10.0.11",
"@angular/core": "~10.0.11",
"@angular/forms": "~10.0.11",
"@angular/platform-browser": "~10.0.11",
"@angular/platform-browser-dynamic": "~10.0.11",
"@angular/router": "~10.0.11",
"@mdi/font": "^5.6.55",
"@tinymce/tinymce-angular": "^4.2.0",
"materialize-css": "^1.0.0-rc.2",
"ngx-materialize": "^6.1.3",
"rxjs": "~6.5.5",
"tslib": "^2.0.0",
"zone.js": "~0.10.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1000.7",
"@angular/cli": "~10.0.7",
"@angular/compiler-cli": "~10.0.11",
"@types/node": "^12.11.1",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~3.3.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~3.9.5"
}
}
这里是angular.json
"styles": [
"src/styles.css",
"node_modules/materialize-css/dist/css/materialize.min.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/materialize-css/dist/js/materialize.min.js",
"node_modules/@mdi/font/css/materialdesignicons.min.css"
]
},
这是我的组件
import { Component, Input, OnInit } from '@angular/core';
declare const M: any;
@Component({
selector: 'app-recommended-posts',
templateUrl: './recommended-posts.component.html',
styleUrls: ['./recommended-posts.component.css']
})
export class RecommendedPostsComponent implements OnInit {
constructor) { }
ngOnInit(): void {
M.AutoInit();
}
}
和html文件:
<div class="carousel">
<a class="carousel-item" href="#one!">test message</a>
<a class="carousel-item" href="#two!"><img src="https://lorempixel.com/250/250/nature/2"></a>
<a class="carousel-item" href="#three!"><img src="https://lorempixel.com/250/250/nature/3"></a>
<a class="carousel-item" href="#four!"><img src="https://lorempixel.com/250/250/nature/4"></a>
<a class="carousel-item" href="#five!"><img src="https://lorempixel.com/250/250/nature/5"></a>
</div>
我尝试将 js 代码放入 html 页面,甚至 jquery 来自 materilize 网站的代码。但也无济于事。
这个解决方案我在这个网站上查找了一个问题,但结果是一样的。
好像都是导入的,不知道为什么不行。我的错误是什么?谢谢!
看看这个例子(不是我的)。我认为问题在于您正在从 ngOnInit 初始化轮播,但现在还为时过早,因为此时不会呈现子组件。
https://stackblitz.com/edit/angular-materialize-carousel?file=src%2Fapp%2Fapp.component.ts
我是 Angular 的新手,我遇到了问题。我成功地将 materialize 导入 Angular,materialize css 元素有效,但 js 元素无效。轮播和其他元素(例如 select)没有出现在我的组件中。只是一个空位。
这里是package.json
"private": true,
"dependencies": {
"@angular/animations": "^10.0.14",
"@angular/common": "~10.0.11",
"@angular/compiler": "~10.0.11",
"@angular/core": "~10.0.11",
"@angular/forms": "~10.0.11",
"@angular/platform-browser": "~10.0.11",
"@angular/platform-browser-dynamic": "~10.0.11",
"@angular/router": "~10.0.11",
"@mdi/font": "^5.6.55",
"@tinymce/tinymce-angular": "^4.2.0",
"materialize-css": "^1.0.0-rc.2",
"ngx-materialize": "^6.1.3",
"rxjs": "~6.5.5",
"tslib": "^2.0.0",
"zone.js": "~0.10.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1000.7",
"@angular/cli": "~10.0.7",
"@angular/compiler-cli": "~10.0.11",
"@types/node": "^12.11.1",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~3.3.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~3.9.5"
}
}
这里是angular.json
"styles": [
"src/styles.css",
"node_modules/materialize-css/dist/css/materialize.min.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/materialize-css/dist/js/materialize.min.js",
"node_modules/@mdi/font/css/materialdesignicons.min.css"
]
},
这是我的组件
import { Component, Input, OnInit } from '@angular/core';
declare const M: any;
@Component({
selector: 'app-recommended-posts',
templateUrl: './recommended-posts.component.html',
styleUrls: ['./recommended-posts.component.css']
})
export class RecommendedPostsComponent implements OnInit {
constructor) { }
ngOnInit(): void {
M.AutoInit();
}
}
和html文件:
<div class="carousel">
<a class="carousel-item" href="#one!">test message</a>
<a class="carousel-item" href="#two!"><img src="https://lorempixel.com/250/250/nature/2"></a>
<a class="carousel-item" href="#three!"><img src="https://lorempixel.com/250/250/nature/3"></a>
<a class="carousel-item" href="#four!"><img src="https://lorempixel.com/250/250/nature/4"></a>
<a class="carousel-item" href="#five!"><img src="https://lorempixel.com/250/250/nature/5"></a>
</div>
我尝试将 js 代码放入 html 页面,甚至 jquery 来自 materilize 网站的代码。但也无济于事。
这个解决方案我在这个网站上查找了一个问题,但结果是一样的。
好像都是导入的,不知道为什么不行。我的错误是什么?谢谢!
看看这个例子(不是我的)。我认为问题在于您正在从 ngOnInit 初始化轮播,但现在还为时过早,因为此时不会呈现子组件。
https://stackblitz.com/edit/angular-materialize-carousel?file=src%2Fapp%2Fapp.component.ts