Qi Haohan describes with pride the times he has leaped and pirouetted with American dancers across stages in China, and he counts as a major influence Daniil Simkin, a principal dancer with the American Ballet Theater.

Ask him about China’s trade war with the United States, however, and Mr. Qi’s admiration for America evaporates.

“Fight, fight, fight!” the 25-year-old wrote on social media, urging his country to stand strong after trade talks with the United States broke down.

“America’s decision to increase tariffs will only bring about its own destruction,” Mr. Qi, a dancer with the National Ballet of China, said in an interview. “China is totally ready to respond.”

Mr. Qi’s views are an example of the complex, sometimes contradictory attitudes toward the United States held across China — a love-hate relationship that presents an unusual challenge to the ruling Communist Party and its leader, Xi Jinping, as they try to defend their image at home amid the bruising trade war.

Divided popular opinion — and ambivalence about America, even among some of its most ardent fans (and critics) — makes it difficult for Beijing to come down too hard on the United States. But if it does too little, the party risks looking weak.

Chinese people have long looked to America as a source of inspiration, with its gleaming skyscrapers, financial power and unparalleled military might. But they also increasingly see it as a strategic rival — a view partly fueled by pride in China’s rise, and by the party’s propaganda organs, which have long depicted America as a hostile, imperialist country that has tried to keep China down.

“China now has the No. 2 mentality,” said Yun Sun, a China analyst at the Washington-based Stimson Center. “It’s only natural for No. 2 to want to surpass No. 1.”

Even in China’s authoritarian political system, public opinion must be carefully managed. If leaders push an anti-American message too far, they run the risk of nationalist sentiment spiraling out of control. That would limit their options in talks with Washington by forcing them to adopt a tough posture.

Though China has ways to prop up its economy, there are deep-seated concerns that it is not ready for a prolonged standoff, which could exact a heavy toll on people’s livelihoods. That could ultimately backfire on the party, which has staked its legitimacy on generating continuous economic growth.

On the other hand, if Chinese leaders act too cautiously, they could look inept to a domestic populace that has, in recent years, become more self-assured about China’s status as a rising power.

What was once starry-eyed enthusiasm for America among many Chinese has given way to sober admiration, if not outright disillusionment, as people have gotten to know the United States better and its problems have come into clearer view.

According to the latest nationwide survey by the Pew Research Center, published in 2016, 45 percent of Chinese saw American power and influence as a major threat to their country, up from 39 percent in 2013. More than half of Chinese believed the United States was trying to prevent China from becoming as powerful as America, the survey found.

That trend may well have accelerated over the past year, which has seen the world’s two largest economies go head-to-head in a protracted trade war and a dispute over Huawei, the Chinese telecommunications giant. The United States has also tightened restrictions on visas for Chinese students and visiting scholars, measures it says are aimed at curbing intellectual property theft and spying.

Such developments have reinforced the Chinese perception that the United States is deliberately thwarting their country’s rightful rise — leaving China with no choice but to fight back.

“We are not scared. China has money,” said Amanda Lin, 36, as she sipped an Americano at a Starbucks in Beijing. She said the Chinese manufacturing company she works for had been badly hit by the latest round of tariffs. “Perhaps we have to sacrifice a little in the short term, but if we don’t fight, then we will suffer more in the longer term,” she said.

Skepticism about American intentions taps into China’s collective memory of the 19th century, when Western powers forcibly opened Chinese ports and carved up the country into spheres of influence. Nowadays, China is a fast-modernizing nation, home to a booming middle class and cutting-edge infrastructure.

But many Chinese still remember their outrage when the United States accidentally bombed China’s embassy in Belgrade in 1999, during the war in the former Yugoslavia. The bombing, which killed three Chinese people, prompted days of violent protests. Two years later, tensions flared again when China detained a United States Navy flight crew after a Chinese fighter and an American spy plane collided in midair.

China has ratcheted up anti-American propaganda in recent weeks, but its campaign has been comparatively restrained. Still, the authorities, ever wary of unrest that could be turned against the government, are taking few risks.

Chen Chun, a liberal political columnist in the southern Chinese city of Guangzhou, said he was recently pulled in for a meeting with local security officials who urged him to take a more moderate tone in his writings.

“They said that Chinese people are easily instigated and that emotions can get really complicated,” Mr. Chen said.

“On one hand, the authorities want to use nationalism to legitimize their regime,” he added. “But if the nationalists spin out of control, it can also affect their power and the system’s stability.”

The party may also be reluctant to play up China’s rivalry with America, knowing that affection here for the United States — whose Chinese name means “beautiful country” — still runs deep.

American culture is so deeply embedded in China, experts say, that it would be impossible to boycott the country’s products, as China has done with goods from Japan and South Korea when tensions with those countries ran high. Many Chinese love their iPhones and lobsters imported from Boston, and are fans of American television hits like “House of Cards” and “Modern Family.”

The affinity extends beyond products. Many Chinese still admire America for its education system, strong rule of law and soft-power dominance. Some continue to draw inspiration from the idea of the American dream.

“The American dream means working hard and achieving your goals one step at a time,” said Kobe Liu Zhe, 29, a Kobe Bryant superfan in the northeastern city of Harbin who recently made headlines in America after he unknowingly bought the National Basketball Association star’s stolen high school jersey. (He later returned it.) “Kobe Bryant represents that dream.”

And yet, while the United States remains one of the top destinations for Chinese tourists, business travelers and students, the growth in that traffic is slowing. The increase in the number of Chinese visitors to the United States fell sharply from 16 percent in 2016 to only 4 percent in 2017, according to the United States Commerce Department.

The slowdown has been even more apparent in education. The increase in the number of Chinese students going to America has slowed from a high of nearly 30 percent in 2010 to just 3.6 percent last year, according to the Institute of International Education.

The decline, experts say, partly reflects a growing belief that America’s star is losing its luster.

“Thirty years ago, a lot of people thought that going to the United States was like going to heaven,” said Liu Peng, an education consultant in the eastern city of Qingdao. “But now people think the United States is falling behind while China is growing.”

Even if a trade deal is reached soon, experts say party leaders are bracing for a prolonged period of competition with America. Preparing public opinion for that future, some say, will require adjusting to the younger generation’s increasing cultural confidence.

“The older generation of Chinese both respect and fear the United States, we were brought up to think America was superior and we were the underdog,” said Wang Xiaodong, a nationalist writer. “But the perspective of young Chinese is different. They don’t respect you. Nor are they afraid of you.”