Visual Studio 2017 SPA中ngx-bootstrap dropdown失效问题求助
Let’s walk through the most common fixes for your issue—where the dropdown menu <ul> is being commented out in the DOM despite no console errors. Based on your setup details, here are the key areas to check:
1. Remove BsDropdownModule Import from Your Component
You mentioned importing BsDropdownModule in nav.component.ts—this is unnecessary and can cause issues. Angular modules are meant to be imported at the module level (like app.browser.module.ts), not in individual components.
Delete the import line from nav.component.ts entirely. The dropdown directives (dropdown, dropdownToggle, *dropdownMenu) will be available to the component automatically once the module is correctly imported at the module level.
2. Verify Module Import Structure
Make sure your module imports follow the correct pattern for ngx-bootstrap:
- In your root module (
app.browser.module.ts), useBsDropdownModule.forRoot()to initialize the dropdown module globally:import { BsDropdownModule } from 'ngx-bootstrap/dropdown'; @NgModule({ imports: [ // ... other imports BsDropdownModule.forRoot() ] }) export class AppBrowserModule { } - If your
nav.componentis part of a child module (e.g., aSharedModuleorNavModule), importBsDropdownModule(without.forRoot()) in that child module’simportsarray. This ensures the directives are available to components in that module.
3. Fix Your Webpack Vendor Configuration
Adding ngx-bootstrap to nonTreeShakableModules might not be specific enough. For Angular 4 and ngx-bootstrap 2.x, you need to target the dropdown module directly to ensure its directives are included in the vendor bundle:
Update your webpack.config.vendor.js to include the specific dropdown path:
nonTreeShakableModules: [ // ... other modules 'ngx-bootstrap/dropdown' ]
After making this change, re-run your webpack build to ensure the dropdown directives are properly bundled.
4. Validate Your Template Structure
ngx-bootstrap’s dropdown requires a strict DOM structure for the directives to work correctly. If your template doesn’t match the required pattern, Angular will comment out elements that can’t resolve the directives.
Ensure your template follows this structure:
<!-- Parent container with the `dropdown` directive --> <div class="dropdown" dropdown> <!-- Trigger button with `dropdownToggle` directive --> <button class="btn btn-default" dropdownToggle> Dropdown Trigger </button> <!-- Menu with `*dropdownMenu` structural directive --> <ul *dropdownMenu class="dropdown-menu"> <li><a class="dropdown-item" href="#">Item 1</a></li> <li><a class="dropdown-item" href="#">Item 2</a></li> </ul> </div>
Double-check that:
- The
dropdowndirective is on the parent container of the toggle and menu. dropdownToggleis on the button/element that triggers the dropdown.*dropdownMenuis applied directly to the<ul>element (not a parent or child element).
5. Confirm Version Compatibility
While your versions are theoretically compatible (Angular 4.2.5 + ngx-bootstrap 2.0.0-rc.0), release candidates can have hidden bugs. Try upgrading ngx-bootstrap to the latest stable 2.x version to rule out RC-specific issues:
npm install ngx-bootstrap@2.0.0 --save-dev
Also, ensure Bootstrap 3.3.7 CSS is properly loaded in your app (either via a <link> in index.html or imported in your styles bundle)—though this won’t cause the <ul> to be commented out, it’s necessary for the dropdown to display correctly once the directives work.
内容的提问来源于stack exchange,提问作者Matthew Flynn

